how to set SelectedIndex in DataGridViewComboBoxColumn?

后端 未结 7 845
慢半拍i
慢半拍i 2021-01-18 04:00

i am using a datagridview in that i am using a datagridviewcomboboxcolumn, comboboxcolumn is displaying text but the problem is i want to select the first item of comboboxco

7条回答
  •  执念已碎
    2021-01-18 04:40

    If DataGridViewComboBoxCell already exist:

    DataTable dt = new DataTable();
    dt.Columns.Add("Item");
    dt.Columns.Add("Value");
    dt.Rows.Add("Item 1", "0");
    dt.Rows.Add("Item 2", "1");
    dt.Rows.Add("Item 3", "2");
    dt.Rows.Add("Item 4", "3");
    
    for (int i = 0; i < dvg.Rows.Count; i++)
    {
        DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)dvg.Rows[i].Cells[1];
        comboCell.DisplayMember = "Item";
        comboCell.ValueMember = "Value";
        comboCell.DataSource = dt;
    };
    

提交回复
热议问题