check for empty values in dataviewgrid

krypto6969

Member
Joined
Nov 7, 2012
Messages
7
Programming Experience
Beginner
I'm trying to check certain columns in my datagrid to make sure they are not empty. If they are empty I want to raise a msgbox and then allow user to go back to dataviewgrid.
Trying to do this on the save/click event of my bindingnavigator.
 
If you're using a BindingNavigator then the most likely scenario is that you have populated a DataTable and bound that. In that case, the contents of an "empty" cell will be DBNull.Value. You can test the Value of each cell or, preferably, loop through the DataRowView objects in the BindingSource and test them by column name or index.

By the way, its a DataGridView, not a DataViewGrid (which doesn't exist) or a DataGrid (which is a different control).
 
If you're using a BindingNavigator then the most likely scenario is that you have populated a DataTable and bound that. In that case, the contents of an "empty" cell will be DBNull.Value. You can test the Value of each cell or, preferably, loop through the DataRowView objects in the BindingSource and test them by column name or index.

By the way, its a DataGridView, not a DataViewGrid (which doesn't exist) or a DataGrid (which is a different control).


got it. .. thank you sir. Much appreciated.
 
Another mechanism, assuming a MyDataGridView is bound to a MyDataTable is as follows:

If String.IsNullOrWhiteSpace(MyDataTable.Rows(r).Item(c).ToString) Then
    ' Cell is empty or white space
Else
    ' Cell has something in it
End If

If you regard a white space string as a valid "entry" i.e. this means "not empty" then you can use the test

If String.IsNullOrEmpty(MyDataTable.Rows(r).Item(c).ToString) Then
    ' Cell is empty or DBNull
Else
    ' Cell has something in it including possibility of whitespace
End If


Both these mechanisms work without generating DBNull exceptions.
 
Back
Top