Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Still dizzing about validating DataGridView Control...
 

Still dizzing about validating DataGridView Control...

After create a DataGridView control that bound to a table, I still don't know how to validating the input to the DataGridView. My Datagrid :

ID_no Date Name Age Debet Credit

A001 08/06/06 Eric 25 30

A00209/06/06Daniel 23 55

A00315/08/06Catthy 26 35

I create the DataGridViewwith dragingthe data source, after Iadd connection, dataset, bindingsource and bindingnavigator with the wizard....But now when I input a invalid data, example a invalid date, or input alphabet in column Age, I want my messagebox appear to tell the user that it's a invalid data, rather than the error that displayed by system. Also....when the column debet have a value > 0,I want the user cannot input the column credit. How to make it.....???? please help me....thank's..........

Sixon  Friday, November 10, 2006 5:50 PM

Try something like this


Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dcID As New DataGridViewTextBoxColumn
With dcID
.Name = "ID"
.HeaderText = "ID"
End With
Dim dcDate As New DataGridViewTextBoxColumn
With dcDate
.Name = "Date"
.HeaderText = "Date"
End With
Dim dcCredit As New DataGridViewTextBoxColumn
With dcCredit
.Name = "Credit"
.HeaderText = "Credit"
End With
Dim dcDebit As New DataGridViewTextBoxColumn
With dcDebit
.Name = "Debit"
.HeaderText = "Debit"
End With

DataGridView1.Columns.Add(dcID)
DataGridView1.Columns.Add(dcDate)
DataGridView1.Columns.Add(dcDebit)
DataGridView1.Columns.Add(dcCredit)
End Sub

Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
Dim strName As String = DataGridView1.Columns(e.ColumnIndex).Name
If String.IsNullOrEmpty(e.FormattedValue.ToString) Then Return

Select Case strName
Case "Date"
Dim dt As Date
If Not DateTime.TryParse(e.FormattedValue.ToString, dt) Then
MessageBox.Show("Invalid Date")
e.Cancel = True
End If
Case "Debit"
Dim dDebit As Decimal
Dim bValid As Boolean = Decimal.TryParse(e.FormattedValue.ToString, dDebit)
Dim strCredit As String
strCredit = DataGridView1.Item("Credit", e.RowIndex).FormattedValue.ToString
If String.IsNullOrEmpty(e.FormattedValue.ToString) Then Return
If bValid And strCredit.Length < 1 Then
' everything is good
Else
MessageBox.Show("Invalid number or credit not empty")
e.Cancel = True
End If
Case "Credit"
Dim dCredit As Decimal
Dim bValid As Boolean = Decimal.TryParse(e.FormattedValue.ToString, dCredit)
Dim strDebit As String = DataGridView1.Item("Debit", e.RowIndex).FormattedValue.ToString

If bValid And strDebit.Length < 1 Then
' everything is good
Else
MessageBox.Show("Invalid number or debit not empty")
e.Cancel = True
End If
End Select
End Sub
End Class


Ken Tucker  Sunday, November 12, 2006 2:22 AM

Try something like this


Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dcID As New DataGridViewTextBoxColumn
With dcID
.Name = "ID"
.HeaderText = "ID"
End With
Dim dcDate As New DataGridViewTextBoxColumn
With dcDate
.Name = "Date"
.HeaderText = "Date"
End With
Dim dcCredit As New DataGridViewTextBoxColumn
With dcCredit
.Name = "Credit"
.HeaderText = "Credit"
End With
Dim dcDebit As New DataGridViewTextBoxColumn
With dcDebit
.Name = "Debit"
.HeaderText = "Debit"
End With

DataGridView1.Columns.Add(dcID)
DataGridView1.Columns.Add(dcDate)
DataGridView1.Columns.Add(dcDebit)
DataGridView1.Columns.Add(dcCredit)
End Sub

Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
Dim strName As String = DataGridView1.Columns(e.ColumnIndex).Name
If String.IsNullOrEmpty(e.FormattedValue.ToString) Then Return

Select Case strName
Case "Date"
Dim dt As Date
If Not DateTime.TryParse(e.FormattedValue.ToString, dt) Then
MessageBox.Show("Invalid Date")
e.Cancel = True
End If
Case "Debit"
Dim dDebit As Decimal
Dim bValid As Boolean = Decimal.TryParse(e.FormattedValue.ToString, dDebit)
Dim strCredit As String
strCredit = DataGridView1.Item("Credit", e.RowIndex).FormattedValue.ToString
If String.IsNullOrEmpty(e.FormattedValue.ToString) Then Return
If bValid And strCredit.Length < 1 Then
' everything is good
Else
MessageBox.Show("Invalid number or credit not empty")
e.Cancel = True
End If
Case "Credit"
Dim dCredit As Decimal
Dim bValid As Boolean = Decimal.TryParse(e.FormattedValue.ToString, dCredit)
Dim strDebit As String = DataGridView1.Item("Debit", e.RowIndex).FormattedValue.ToString

If bValid And strDebit.Length < 1 Then
' everything is good
Else
MessageBox.Show("Invalid number or debit not empty")
e.Cancel = True
End If
End Select
End Sub
End Class


Ken Tucker  Sunday, November 12, 2006 2:22 AM
Thank you Kent.... It's really solve my problem......
Sixon  Friday, November 17, 2006 8:46 PM

You can use google to search for other answers

Custom Search

More Threads

• 2 relation tables in formview????
• Data Binding Informix DB
• IErrorInfo.GetDescription failed with E_FAIL(0x80004005)
• Handling data in two datagridviews
• problem with dataview sort
• Databinding and textboxes
• MSDN Example can't compile
• Owner of objects in Database
• How can we doing like a menu (pic inside)
• Problem with value DataGridViewComboBoxCell