2008-09-18 12 views
12

मैं DataGridView का उपयोग कर एक टेबल बना रहा हूं जहां कोई उपयोगकर्ता प्रत्येक सेल में ड्रॉपडाउन से आइटम का चयन कर सकता है। समस्या को सरल बनाने के लिए, मान लें कि मेरे पास 1 कॉलम है। मैं डिजाइनर में DataGridViewComboBoxColumn का उपयोग कर रहा हूँ। मैं उस कॉलम में प्रत्येक पंक्ति को चुनने के लिए आइटमों की एक अलग सूची रखने का समर्थन करने की कोशिश कर रहा हूं।DataGridViewComboBoxColumn प्रत्येक पंक्ति में अलग-अलग आइटम जोड़ रहा है।

क्या यह संभव है?

उत्तर

16

हां। यह DataGridViewComboBoxCell का उपयोग करके किया जा सकता है।

पूरे कॉलम की बजाय आइटम को केवल एक सेल में जोड़ने का एक उदाहरण तरीका है।

private void setCellComboBoxItems(DataGridView dataGrid, int rowIndex, int colIndex, object[] itemsToAdd) 
{ 
    DataGridViewComboBoxCell dgvcbc = (DataGridViewComboBoxCell) dataGrid.Rows[rowIndex].Cells[colIndex]; 
    // You might pass a boolean to determine whether to clear or not. 
    dgvcbc.Items.Clear(); 
    foreach (object itemToAdd in itemsToAdd) 
    { 
     dgvcbc.Items.Add(itemToAdd); 
    } 
} 
+0

इस कोड को एक परियोजना है कि मैं इस समय पर काम कर रहा हूँ के लिए बहुत अच्छा काम किया। –

2
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
{ 
    if (e.ColumnIndex == DataGridViewComboBoxColumnNumber) 
    { 
     setCellComboBoxItems(myDataGridView, e.RowIndex, e.ColumnIndex, someObj); 
    } 
} 
1

बस मामले में किसी को भी इस सूत्र पाता है, इस वीबी 2008 में मेरी हल है लाभ यह प्रदान करता है कि यह आपको बता गया में प्रत्येक मान को एक ID निर्दिष्ट करने की अनुमति देता है।

Private Sub FillGroups() 
    Try 
     'Create Connection and SQLCommand here. 

     Conn.Open() 
     Dim dr As SqlDataReader = cm.ExecuteReader 

     dgvGroups.Rows.Clear() 

     Dim PreviousGroup As String = "" 

     Dim l As New List(Of Groups) 

     While dr.Read 

      Dim g As New Groups 
      g.RegionID = CheckInt(dr("cg_id")) 
      g.RegionName = CheckString(dr("cg_name")) 
      g.GroupID = CheckInt(dr("vg_id")) 
      g.GroupName = CheckString(dr("vg_name")) 
      l.Add(g) 

     End While 
     dr.Close() 
     Conn.Close() 

     For Each a In (From r In l Select r.RegionName, r.RegionID).Distinct 

      Dim RegionID As Integer = a.RegionID 'Doing it this way avoids a warning 

      dgvGroups.Rows.Add(New Object() {a.RegionID, a.RegionName}) 

      Dim c As DataGridViewComboBoxCell = CType(dgvGroups.Rows(dgvGroups.RowCount - 1).Cells(colGroup.Index), DataGridViewComboBoxCell) 
      c.DataSource = (From g In l Where g.RegionID = RegionID Select g.GroupID, g.GroupName).ToArray 
      c.DisplayMember = "GroupName" 
      c.ValueMember = "GroupID" 
     Next 

    Catch ex As Exception 
    End Try 
End Sub 

Private Class Groups 

    Private _RegionID As Integer 
    Public Property RegionID() As Integer 
     Get 
      Return _RegionID 
     End Get 
     Set(ByVal value As Integer) 
      _RegionID = value 
     End Set 
    End Property 

    Private _RegionName As String 
    Public Property RegionName() As String 
     Get 
      Return _RegionName 
     End Get 
     Set(ByVal value As String) 
      _RegionName = value 
     End Set 
    End Property 

    Private _GroupName As String 
    Public Property GroupName() As String 
     Get 
      Return _GroupName 
     End Get 
     Set(ByVal value As String) 
      _GroupName = value 
     End Set 
    End Property 

    Private _GroupID As Integer 
    Public Property GroupID() As Integer 
     Get    
      Return _GroupID 
     End Get 
     Set(ByVal value As Integer) 
      _GroupID = value 
     End Set 
    End Property 

End Class 
0

इस जो 2 comboboxColumns है और जब एक comboBoxColumns1 चयनित सूचकांक बदल तो डेटाबेस से दो अलग-अलग स्तंभों से से डेटा के साथ comboBoxColumns2 लोड GridView के साथ एक उदाहरण है।

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
     { 
       if (dataGridView1.Rows[e.RowIndex].Cells[0].Value != null && dataGridView1.CurrentCell.ColumnIndex == 0) 
      { 

       SqlConnection conn = new SqlConnection("data source=.;initial catalog=pharmacy;integrated security=true"); 
       SqlCommand cmd = new SqlCommand("select [drugTypeParent],[drugTypeChild] from [drugs] where [drugName]='" + dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString() + "'", conn); 
       conn.Open(); 
       SqlDataReader dr = cmd.ExecuteReader(); 
       while (dr.Read()) 
       { 

        object[] o = new object[] { dr[0].ToString(),dr[1].ToString() }; 
        DataGridViewComboBoxCell dgvcbc = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[1]; 

        dgvcbc.Items.Clear(); 
        foreach (object itemToAdd in o) 
        { 
         dgvcbc.Items.Add(itemToAdd); 
        } 
       } 
       dr.Close(); 
       conn.Close(); 
       } 
      } 
-1
//Populate the Datatable with the Lookup lists 
    private DataTable typeDataTable(DataGridView dataGridView, Lookup<string, Element> type_Lookup, Dictionary<Element, string> type_dictionary, string strNewStyle, string strOldStyle, string strID, string strCount) 
    { 
     int row = 0; 

     DataTable dt = new DataTable(); 

     dt.Columns.Add(strOldStyle, typeof(string)); 
     dt.Columns.Add(strID, typeof(string)); 
     dt.Columns.Add(strCount, typeof(int)); 
     dt.Columns.Add("combobox", typeof(DataGridViewComboBoxCell)); 



     //Add All Doc Types to ComboBoxes 
     DataGridViewComboBoxCell CmBx = new DataGridViewComboBoxCell(); 
     CmBx.DataSource = new BindingSource(type_dictionary, null); 
     CmBx.DisplayMember = "Value"; 
     CmBx.ValueMember = "Key"; 


     //Add Style Comboboxes 
     DataGridViewComboBoxColumn Data_CmBx_Col = new DataGridViewComboBoxColumn(); 
     Data_CmBx_Col.HeaderText = strNewStyle; 
     dataGridView.Columns.Add(addDataGrdViewComboBox(Data_CmBx_Col, type_dictionary)); 

     setCellComboBoxItems(dataGridView, 1, 3, CmBx); 

     //Add style Rows 
     foreach (IGrouping<string, Element> StyleGroup in type_Lookup) 
     { 
      row++; 
      //Iterate through each group in the Igrouping 
      //Add Style Rows 
      dt.Rows.Add(StyleGroup.Key, row, StyleGroup.Count().ToString()); 


     } 
     return dt; 
    } 




    private void setCellComboBoxItems(DataGridView dataGrid, int rowIndex, int colIndex, DataGridViewComboBoxCell CmBx) 
    { 
     DataGridViewComboBoxCell dgvcbc = (DataGridViewComboBoxCell)dataGrid.Rows[rowIndex].Cells[colIndex]; 
     // You might pass a boolean to determine whether to clear or not. 
     dgvcbc.Items.Clear(); 
     foreach (DataGridViewComboBoxCell itemToAdd in CmBx.Items) 
     { 
      dgvcbc.Items.Add(itemToAdd); 
     } 
+1

मैं आपको अपने कोड नमूने के लिए स्पष्टीकरण प्रदान करने की सलाह दूंगा। – Wtower

संबंधित मुद्दे