-
February 23rd, 2012, 07:47 PM
#1
Check if DataGridView is empty.
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.
-
February 23rd, 2012, 08:03 PM
#2
Re: Check if DataGridView is empty.
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.
-
February 23rd, 2012, 09:38 PM
#3
Re: Check if DataGridView is empty.
I'm not sure that you can add null items to a ComboBox...?
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
February 23rd, 2012, 09:50 PM
#4
Re: Check if DataGridView is empty.
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 10: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.
-
February 24th, 2012, 07:59 PM
#5
Re: Check if DataGridView is empty.
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.
-
February 25th, 2012, 12:30 AM
#6
Re: Check if DataGridView is empty.
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 01: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.
-
February 25th, 2012, 11:50 AM
#7
Re: Check if DataGridView is empty.
That makes sense, sorry but I need to see a code example to implement.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|