Set the selected item for a combobox in a datagrid
Posted
by
JingJingTao
on Stack Overflow
See other posts from Stack Overflow
or by JingJingTao
Published on 2011-01-07T10:23:31Z
Indexed on
2011/01/07
10:54 UTC
Read the original article
Hit count: 323
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;
© Stack Overflow or respective owner