471,349 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

WinForms Datagrid Validation VB.NET


Does anyone know how you can prevent a row full of 'null' values from being
added to the datagrid when a user hits the delete key, deleting the word
(null) which represents the default value of 'null'?

Here's a simple way to reproduce this problem:
1) Add a datagrid to a winform
2) Put this code into the Form1_Load event and run it.

Dim dc1, dc2, dc3 As DataColumn
Dim ds As New DataSet
Dim dt As New DataTable

dc1 = New DataColumn("FirstName")
dc2 = New DataColumn("LastName")
dc3 = New DataColumn("Age")


DataGrid1.DataSource = dt

3) Delete the first (null) from the first column, then click into the next

As you should see, the first row is left waiting to be updated back to the
datasource with nulls and an empty string. I know that the grid considers
the empty string to be valid user input so it allows it.
However, the problem also occurs if the datagrid were bound to a dataset
that specified the Age column to be an integer (FYI - The dataset I actually
use is populated by a SQL Stored Procedure). Then if you delete the (null)
from the Age column OR type a non numeric character and then click to the
next row, the grid will again allow the row full of nulls to remain except
this time all the columns are nulls.

What seems to happen in the case when using a bound datagrid with an integer
column is what is specified as automatic validation:

.... which results in a System.FormatException "Input string was not in a
correct format." error.

I would like to be able to prevent a null row from being added (and
remaining) to the datagrid.

The ways I've thought of trying to accomplish this are:
1) Validate the data entered when the user clicks out of the cell. I think
I would need to create my own class that derived from the DatagridTextBox
class and add an event handler to trap this?

2) Prevent certain keystrokes. But I still want to allow users to use the
delete and backspace keys when appropriate.

3) Iterate through the rows and columns of the datagrid to programatically
delete any rows that have been left with all nulls or empty strings.
However, this seems kind of sloppy and would probably be confusing to the

Any ideas regarding this would be greatly appreciated!!!
Jul 27 '05 #1
0 4278

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by John Spiegel | last post: by
1 post views Thread by GregM | last post: by
1 post views Thread by Stephan Bour | last post: by
5 posts views Thread by Chris | last post: by
4 posts views Thread by siaj | last post: by
5 posts views Thread by Tina | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.