简体   繁体   中英

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.

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;

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 .

-- Pavel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM