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.
Printable View
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 ))
{
//.....
}
}
I'm not sure that you can add null items to a ComboBox...?
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.
Thanks for your help, the problem is I have that I don't know how many rows there are. I want to make sure that none of the combo boxes are null.
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?
That makes sense, sorry but I need to see a code example to implement.