By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,315 Members | 1,672 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,315 IT Pros & Developers. It's quick & easy.

WinForms Datagrid Validation VB.NET

P: n/a
JR
Hello,

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")

dt.Columns.Add("FirstName")
dt.Columns.Add("LastName")
dt.Columns.Add("Age")

ds.Tables.Add(dt)
DataGrid1.DataSource = dt

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

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:
http://msdn.microsoft.com/library/de...ridcontrol.asp

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

Any ideas regarding this would be greatly appreciated!!!
Jul 27 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.