CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2011
    Posts
    11

    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.

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    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.

  3. #3
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    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.......

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    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.

  5. #5
    Join Date
    Nov 2011
    Posts
    11

    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.

  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    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.

  7. #7
    Join Date
    Nov 2011
    Posts
    11

    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
  •  





Click Here to Expand Forum to Full Width

Featured