I have a program that creates a data grid view with a user-inputted amount of rows. One column is a combo box, and the other two are check boxes. How can I determine if any of the values of the combo boxes are null? It's not connected to a datagrid.
is it a windows form gridview? Then you should have a combobox column, taht is a DataGridViewComboBoxColumn .
Thus you should retrieve the row, retrieve the column casting it to DataGridViewComboBoxColumn
Code:
//for example, in CellValidated event of the grid:
DataGridViewComboBoxColumn myCboCell = (DataGridViewComboBoxCell)myGrid.CurrentRow.Cells["MyCboCell"];
if (myCboCell .Value != null)
{
string theSelectedValue =myCboCell.Value.ToString();
if(!String.IsnullOrEmpty(theSelectedValue ))
{
//.....
}
}
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
if it has the proper valuemember, it could be null (not dbNull, however). for the displaymember, it is a string and it can be null. But you do not need to have it null, you can have it as an empty string. Usually, i do these trasnformations directly in the query or sp that brings the data, selecting using the Coalesce clause in sql Server . But more likely, if the source is null, the datacolumn will display an empty drop down. In this case I have overriden the Paint event to not draw the dropdown at all in that row. To do this, I derived a DisabledDataGridViewComboBoxColumn from a DataGridViewComboBoxColumn following trhe example here and adapting for combocolumn http://msdn.microsoft.com/en-us/library/ms171619.aspx
**edit
to tell the truth, i think you're right: I looked at a project where I ahd it and I used the datatsource to fill a list of object to bound to the column. if the list was empty, i did not paint the combobox. And I could have one empty string, but ensured never tried to add a null value or duplicated values or empty keys.
Last edited by Cimperiali; February 23rd, 2012 at 09:03 PM.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
you should operate on the DataSource side: when you retrieve data to bind to your Combo, you should ensure you have at least one record. You can do this by always adding the "Please, select something" with key -1 record to the collection of items that populate the combo. Makes sense in your project?
Last edited by Cimperiali; February 25th, 2012 at 12:35 PM.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
Bookmarks