Set the selected item for a combobox in a datagrid
- by JingJingTao
I am using a datagrid which has many combobox fields in it, when I click the datagrid combobox the selected item or highlighted value is the last item in the list, but I would like it to highlight the first(top) item in the list. I know for just a combobox, all I need to do is change the combobox.selecteditem or combobox.selectedindex, but I'm not sure what to do in this case.
I have binded the combobox to a table in the database and used a datatable to store the combobox values and then I add a row to the datatable, I think the reason the last item in the combobox is highlighted is because I added a row to the datatable.
Thank you for your help.
String strGetTypes = "SELECT holidaycodeVARCHAR4Pk, codedescVARCHAR45
FROM holidaytype
ORDER BY holidaycodeVARCHAR4Pk Desc";
DataTable dtHolidayType = new DataTable();
MySqlDataAdapter dbaElements = new MySqlDataAdapter(strGetTypes, ShareSqlSettings.dbConnect);
dbaElements.Fill(dtHolidayType);
DataGridViewComboBoxCell cboxDays = new DataGridViewComboBoxCell();
cboxDays.DataSource = dtHolidayType;
cboxDays.DisplayMember = "codedescVARCHAR45";
cboxDays.ValueMember = "holidaycodeVARCHAR4Pk";
//Blank row
dtHolidayType.Rows.Add(1);
//
gridDailyEmp.Rows[j].Cells[day] = cboxDays;