Set the selected item for a combobox in a datagrid
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.
String strGetTypes = "SELECT holidaycodeVARCHAR4Pk, codedescVARCHAR45
FROM holidaytype
ORDER BY holidaycodeVARCHAR4Pk Desc";
DataTable dtHolidayType = new DataTable();
MySqlDataAdapter dbaElements = new MySqlDataAdapter(st开发者_JAVA百科rGetTypes, 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;
Maybe you could set the Value
property of the DataGridViewComboBoxCell
object so that it will contain the currently selected value:
cBoxDay.Value = [get the holiday code for the current record];
Read more on MSDN.
精彩评论