DataGridView and Combo Boxes (my brain hurts)
-
I mostly use ASP.net but have spent the day wrestling with a date view grid. I have a column which has a dropdown so when I populate the grid I programatically add a combo (DataGridViewComboBoxCell) to the row and can populate the items with data. The column is a normal DataViewGridTextBoxColumn because I want to have a completely free hand in what I put in the cells in this column. But... the dropdowns have a blank entry at the top which I'd like to get rid of and also there is no property to select an item in the dropdown such as selectedIndex. How han I get rid of the space at the top (blank entry) and how can I set the selected index property? Also, when I want to read the value from the combo and have a reference to the DataGridViewComboBoxCell object in that cell, how do I read the selected value from the combo?
-
I mostly use ASP.net but have spent the day wrestling with a date view grid. I have a column which has a dropdown so when I populate the grid I programatically add a combo (DataGridViewComboBoxCell) to the row and can populate the items with data. The column is a normal DataViewGridTextBoxColumn because I want to have a completely free hand in what I put in the cells in this column. But... the dropdowns have a blank entry at the top which I'd like to get rid of and also there is no property to select an item in the dropdown such as selectedIndex. How han I get rid of the space at the top (blank entry) and how can I set the selected index property? Also, when I want to read the value from the combo and have a reference to the DataGridViewComboBoxCell object in that cell, how do I read the selected value from the combo?
From MSDN: "Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property." I am not sure what will happen when you set the value, although I will assume that it will change the selection. You can then read the Value property to get the value back. There is a way you can make your own selected index property by comparing each item to the Value property and returning the index when the objects match. Hope this helps.