Validation in DataGrid ?

A

Arsalan

How do I validate inputs in DataGrid ? I want to check for constraint and
change columns value dynamically,

for e.g i've column named employee, annual salary and salary

I want the columns annual salary to be dynamically changed when user changes
salary in datagrid

How do I do it?
 
M

Michael C#

I've done something similar with the DataGrid's .CurrentCellChanged event.
It might look something like this:

Private Sub dgPayroll_CurrentCellChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dgPayroll.CurrentCellChanged
' This routine automatically calculates the Yearly Salary based on the
' Monthly Pay amounts entered

' First we locate our current edited cell and get the row
Dim curGrid As DataGrid = CType(sender, DataGrid)
Dim curRow As Integer = curGrid.CurrentCell.RowNumber

' Then we get the gross pay and withholding from that row and
' calculate the net pay. We put it in a try..catch block to catch
' null values.

Try
' Assumes Monthly Pay is entered in column 1
' and Yearly Salary goes into column 2
Dim mothlyPay As Single = Convert.ToSingle(curGrid.Item(curRow, 1))
curGrid.Item(curRow, 2) = monthlyPay * 12.0
Catch
End Try
End Sub
 
K

Ken Tucker [MVP]

Hi,

First add a tablestyle to the datagrid then I would add a handler to
the datagridtextboxcolumns textbox validating event.

Add Tablestyle
http://msdn.microsoft.com/library/d...tingwindowsformsdatagridvisualbasicprimer.asp

http://msdn.microsoft.com/library/d...ry/en-us/dnwinforms/html/wnf_custdatagrid.asp

Add a handler to the textbox validating event


Dim colDescription As New DatagridTextboxcolumn

With colDescription

..MappingName = "Notes"

..HeaderText = "Notes"

..Width = 350

End With

AddHandler colDescription.TextBox.Validating, AddressOf CellValidating



Private Sub CellValidating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs)

Debug.WriteLine(DirectCast(sender, DataGridTextBox).Text)

End Sub


Ken

------------------------
How do I validate inputs in DataGrid ? I want to check for constraint and
change columns value dynamically,

for e.g i've column named employee, annual salary and salary

I want the columns annual salary to be dynamically changed when user changes
salary in datagrid

How do I do it?
 
M

Michael C#

I tell you, I learn something new every time I visit this board! :) Just
one question - how can you update the yearly salary cell on the same row to
reflect the re-calculated salary?

Thanks!
 

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