Suppose there is a datagridview , where there are 2 columns with the type datagridviewcombobox (hereafter cb ).

It is necessary to make that the values ​​in the second cb depend on the choice of the value in the first cb .

The problem is that when I create the second line and select a different value in the first cb , then in the second cb first line, the value of cb since cb affects the entire datagridviewcombobox .

Can this be avoided?

    1 answer 1

    Create and load each combo box separately.

     private void Form1_Load(object sender, EventArgs e) { dataGridView1.ColumnCount = 2; DataGridViewRow row = new DataGridViewRow(); row = GetComboBox(); dataGridView1.Rows.Add(row); dataGridView1.CellValueChanged += dataGridView1_CellValueChanged; } void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { dataGridView1.Rows[e.RowIndex].Cells[1] = GetCell(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()); } private DataGridViewRow GetComboBox() { DataGridViewRow rowDG = new DataGridViewRow(); rowDG.CreateCells(dataGridView1); DataGridViewComboBoxCell cell_CB1 = new DataGridViewComboBoxCell(); cell_CB1.Items.AddRange("Значение ComboBox'a 1"); cell_CB1.Items.AddRange("Значение ComboBox'a 2"); cell_CB1.Items.AddRange("Значение ComboBox'a 3"); DataGridViewComboBoxCell cell_CB2 = new DataGridViewComboBoxCell(); rowDG.Cells[0] = cell_CB1; rowDG.Cells[1] = cell_CB2; return rowDG; } private DataGridViewComboBoxCell GetCell(string value) { DataGridViewComboBoxCell cell_CB2 = new DataGridViewComboBoxCell(); switch(value) { case "Значение ComboBox'a 1": cell_CB2.Items.AddRange("Значение ComboBox'a 101"); cell_CB2.Items.AddRange("Значение ComboBox'a 102"); cell_CB2.Items.AddRange("Значение ComboBox'a 103"); break; case "Значение ComboBox'a 2": cell_CB2.Items.AddRange("Значение ComboBox'a 201"); cell_CB2.Items.AddRange("Значение ComboBox'a 202"); cell_CB2.Items.AddRange("Значение ComboBox'a 203"); break; default: cell_CB2.Items.AddRange("Значение ComboBox'a 301"); cell_CB2.Items.AddRange("Значение ComboBox'a 302"); cell_CB2.Items.AddRange("Значение ComboBox'a 303"); break; } return cell_CB2; } 
    • Accordingly, if the user creates new lines, then I should add a handler for creating ComboBoxes for each event adding a line? - iluxa1810
    • I updated the code, and yes, in the event of creating a new line, add my combo boxes. - DartAlex
    • Or you can use an example that illustrates the following scenario: A List <> with two fields is connected to a DataGridView and a custom ComboBox with random values ​​is generated for each field? - iluxa1810 September
    • @ iluxa1810, you can. But it seems to me to be correct to ask a new question. - DartAlex