datagrid validation

T

Tom

I have seen articles saying that validating the data a
user enters into a datagrid is difficult, because it is
not the datagrid itself that does the validation, but
rather textboxes that are in the cells. The article then
says that it is best to validate the datatable behind the
grid using row or column changing events.

I have a few questions about this.

If I am to use row or column data changing events on the
underlying table, is there a way I can keep those event
routines from being called when my program changes the
data in the table while they are still called when the
user modifies the data via the datagrid?

I find that if I bind to the datagrid a table that is in a
dataset that was loaded from a database via a data adapter
and one of the fields is numeric, that the datagrid
magically won't allow non-numeric entries in that column.
This is exactly what I want. How can I get this
functionality for a datatable I have not loaded from a
database?

Is there someway I can have the datagrid use my own
validatedTextBox (which inherits from textBox) for the
data entry in the cells that would then allow me to do the
validation I want.

By-the-way, all I need to do is make sure that valid
numbers have been entered in a cell. Any other way to do
this in a simple fashion would be welcome.

Thanks
 
C

Cor Ligthert

Tom,

The validating event is fired before a control loses his focus.

In the datagrid you have for that the rowchange and the column change event,
with the exception that that does not fire when there is a button event or
something like that.

However maybe this sample I made gives you some other ideas, it is not
completly your question, however should have the same results when other
events are used in my opinion.
(not tested)

Cor

\\\
Private WithEvents dtbCol1 As DataGridTextBox
Private ToolTip1 As New ToolTip
')
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Datagrid1.ReadOnly = True
Dim ts As New DataGridTableStyle
ts.MappingName = ds.Tables(0).TableName
Dim column As New DataGridTextBoxColumn
ts.GridColumnStyles.Add(column)
DataGrid1.TableStyles.Add(ts)
column = DirectCast(ts.GridColumnStyles(0), DataGridTextBoxColumn)
dtbCol1 = DirectCast(column.TextBox, DataGridTextBox)
column.MappingName = ds.Tables(0).Columns(0).ColumnName
column.HeaderText = "Cor"
column.Width = 30
dv = New DataView(ds.Tables(0))
dv.AllowNew = False
DataGrid1.DataSource = dv
End Sub
Private Sub dtbCol1_ToolTip(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles dtbCol1.MouseEnter
ToolTip1.SetToolTip(DirectCast(sender, DataGridTextBox), _
"Row: " & DataGrid1.CurrentRowIndex + 1)
End Sub
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top