ComboBoxCell doesn't support SelectedIndex property, I have found two way to achieve.
1. Set the initial value of the ComboBoxCell.
For example, here is a table with CountryID and CountryName
private DataTable dtCountry = new DataTable();
dtCountry.Columns.Add("CountryID", typeof(int));
dtCountry.Columns.Add("CountryName", typeof(string));
dtCountry.Rows.Add(1, "Canada");
dtCountry.Rows.Add(2, "USA");
dtCountry.Rows.Add(3, "Mexico");
I will create a new DataGridViewComboBoxCell instance and set its initial value with the first row in dtCountry.
DataGridViewComboBoxCell dgvCmbCell = new DataGridViewComboBoxCell();
dgvCmbCell.DataSource = dtCountry;
dgvCmbCell.DisplayMember = "CountryName";
dgvCmbCell.ValueMember = "CountryID";
dataGridView1.Rows[0].Cells[1] = dgvCmbCell;
if (dataGridView1.Rows[0].Cells[1].Value.ToString() == "")
{
dgvCmbCell.Value = dtCountry.Rows[0]["CountryID"];
}
2. Set the SelectedIndex to 0 when you click the cell and want to edit it. So you need to handle EditingControlShowing event.
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cmb = e.Control as ComboBox;
if (cmb != null)
{
cmb.SelectedIndex = 0;
}
}
Please check out more at this:-
http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/57df7b19-4929-47e3-9793-9a6fba278595/[^][
^]