Question Datagridview Cell Value Type Validate

tonifirnandes

Member
Joined
Mar 7, 2013
Messages
20
Programming Experience
1-3
Hello All the best programmer,
Good Morning and Sorry for disturbing.
i have some problems all, hopely you would like to help and could help me thanks. my problem is :
i need validate or check the data or value in datagridview cell before it update to the database, so i can check if the user input data correctly or not.
i just would like to check, the type data that user input to the cell, is that matching to the sqldatatyp or not.
i just tried to use this code below :
Object.ReferenceEquals(DataGridView1.CurrentCell.Value.GetType(), SqlDbType.VarChar) to check if the type of the cell is the same with the type to the database.
but i always get wrong, i geuss that if the type in datagridview cell is not same with sqldbtype so it always get not same.
hopely you would understand about that, and i need your help thanks.



regards.
 
Please keep each thread to a single topic and each topic to a single thread. Your duplicate thread has been deleted.

What you're trying to do is wrong for several reasons. First of all, SqlDbType.VarChar is not a Type so it can never be the same as the object returned by your GetType call. If you have retrieved data from a SQL Server database column with data type varchar then that data will be stored as Strings in your VB app. The same goes for data types char, nchar, nvarchar and text. They all represent text data and text data in VB is stored in Strings. So, if you want to know whether the data in a particular cell is a String then do one of the following:
VB.NET:
If DataGridView1.CurrentCell.Value.GetType() Is GetType(String) Then
VB.NET:
If TypeOf DataGridView1.CurrentCell.Value Is String Then
 
Hi,

I see this has now been answered but I will post post anyway to see if it adds further value.

The answer to this is taken care of for you if you have created your DataGridView by dragging a DataGridView object from the Data Sources window onto your Form to represent the Table in your Database. This is because the ValueType of the DataGridView's columns is then set to the same type as that in your Database's Table. ([Edit] I should say compatible type to be clear!)

If you have added a DataGridView control to your Form and then populated your DataGridView through code then you can achieve the same functionality above by explicitly setting the ValueType of each DataGridView column. i.e:-

VB.NET:
DataGridView1.Columns(0).ValueType = GetType(Integer)

This will set the first column in the DataGridView to only allow Integer values.

Once either of the above is then done, each time you enter a value into a cell and try to move away from that cell the data in the cell is validated to ensure it conforms to the correct Data Type expected by the cell. If the data type in the cell is incorrect the DataGridView Default Error Dialog will be shown displaying the error. If you want to override this default error dialog then you would code the DataGridView DataError event. i.e:-

VB.NET:
Private Sub DataGridView1_DataError(sender As Object, e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
  MsgBox("Data Error In Row " & e.RowIndex.ToString & ", Column " & e.ColumnIndex.ToString)
End Sub

Hope that helps.

Cheers,

Ian
 
Last edited:
Back
Top