DataGridViewComboBoxColumn sets the selected index

hi i run-time bind data to combotox datagridview. But how to do this to automatically display the first item? I cannot find the selected index from a DataGridViewComboBoxColumn.

DataGridViewComboBoxColumn cbStudentCourse = (DataGridViewComboBoxColumn)dgStudentCourse.Columns["studentCourseStatus"]; cbStudentCourse.DataSource = Enum.GetValues(typeof(CourseStudentStatus)); cbStudentCourse.DisplayIndex = 1; 

- Update ---
I saw someone doing this in solution 3
Link
Are you sure I need such a long code to just select the first element ??????

+6
c # winforms gridview
source share
2 answers

A DataGridViewComboBoxColumn does not have SelectedIndex and SelectedValue properties. However, you can get the same SelectedValue behavior by setting the Value property.

For example, with the first index, the value element has the value 2, then you must set .Value = "2" to set the first selected index.

for example

 myDataGridViewComboBoxColumn.Value = "20"; 

In your case

 myDataGridViewComboBoxColumn.Value = CourseStudentStatus.EnumToBeSelected.ToString(); 

Learn more about DataGridViewComboBoxColumn

+8
source share

best way to set datagridViewComboBoxCell value:

 DataTable dt = new DataTable(); dt.Columns.Add("Item"); dt.Columns.Add("Value"); dt.Rows.Add("Item1", "0"); dt.Rows.Add("Item1", "1"); dt.Rows.Add("Item1", "2"); dt.Rows.Add("Item1", "3"); DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn(); cmb.DefaultCellStyle.Font = new Font("Tahoma", 8, FontStyle.Bold); cmb.DefaultCellStyle.ForeColor = Color.BlueViolet; cmb.FlatStyle = FlatStyle.Flat; cmb.Name = "ComboColumnSample"; cmb.HeaderText = "ComboColumnSample"; cmb.DisplayMember = "Item"; cmb.ValueMember = "Value"; DatagridView dvg=new DataGridView(); dvg.Columns.Add(cmb); cmb.DataSource = dt; for (int i = 0; i < dvg.Rows.Count; i++) { dvg.Rows[i].Cells["ComboColumnSample"].Value = (cmb.Items[0] as DataRowView).Row[1].ToString(); } 

He worked with me very well.

+5
source share

All Articles