Results 1 to 7 of 7

Thread: [RESOLVED] DatagridView Validation

  1. #1

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Resolved [RESOLVED] DatagridView Validation

    I am trying to validate the data entered in a new row and prevent the good data entered being deleted apon a data error being thrown.

    I have spent quick a few hours researching this with no luck.

    I thought this code would select the bad data cell but the entire row is deleted.

    Code:
        Private Sub PaymentsDataGridView_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles PaymentsDataGridView.DataError
            MsgBox("Data Error In Row " & e.RowIndex.ToString & ", Column " & e.ColumnIndex.ToString)
            PaymentsDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = True
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: DatagridView Validation

    If you're trying to validate then shouldn't you be handling the CellValidating and/or RowValidating events? DataError is raised when validation has already failed and gives you an opportunity to clean up. You want to prevent clean up being needed, surely.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: DatagridView Validation

    ok, i have almost got this with the below

    The only issue i am having now is when i start the program and click save the first row gets error icon with the msg("Please enter a paymentAmount") when it shouldnt as the data should be good


    vb.net Code:
    1. 'http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowvalidating.aspx
    2.     Private Sub ValidateByRow(ByVal sender As Object, ByVal data As DataGridViewCellCancelEventArgs) Handles PaymentsDataGridView.RowValidating
    3.         Dim row As DataGridViewRow = PaymentsDataGridView.Rows(data.RowIndex)
    4.         Dim dateCell As DataGridViewCell = row.Cells(PaymentsDataGridView.Columns("dDate").Index)
    5.         Dim descriptionCell As DataGridViewCell = row.Cells(PaymentsDataGridView.Columns("dDescription").Index)
    6.         Dim detailsCell As DataGridViewCell = row.Cells(PaymentsDataGridView.Columns("dDetails").Index)
    7.         Dim paymentMethodCell As DataGridViewCell = row.Cells(PaymentsDataGridView.Columns("pPaymentMethod").Index)
    8.         Dim paymentAmountCell As DataGridViewCell = row.Cells(PaymentsDataGridView.Columns("pPaymentAmount").Index)
    9.         Dim GST_catCell As DataGridViewCell = row.Cells(PaymentsDataGridView.Columns("gGST_cat").Index)
    10.         Dim GSTAmountCell As DataGridViewCell = row.Cells(PaymentsDataGridView.Columns("gGST_amount").Index)
    11.  
    12.         data.Cancel = Not (IsDateGood(dateCell) AndAlso IsDescriptionGood(descriptionCell) _
    13.                            AndAlso IsDetailsGood(detailsCell) AndAlso IsPaymentMethodGood(paymentMethodCell) _
    14.         AndAlso IsPaymentAmountGood(paymentAmountCell) AndAlso IsGST_catGood(GST_catCell) _
    15.         AndAlso IsGSTAmountGood(GSTAmountCell))
    16.     End Sub
    17.  
    18.     Private Function IsDescriptionGood(ByRef cell As DataGridViewCell) As Boolean
    19.         If cell.Value.ToString().Length = 0 Then
    20.             cell.ErrorText = "Please enter a Description"
    21.             PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = "Please enter a Description"
    22.             Return False
    23.         End If
    24.         cell.ErrorText = Nothing
    25.         PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = Nothing
    26.         Return True
    27.     End Function
    28.  
    29.     Private Function IsDetailsGood(ByRef cell As DataGridViewCell) As Boolean
    30.         If cell.Value.ToString().Length = 0 Then
    31.             cell.ErrorText = "Please enter a Detail"
    32.             PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = "Please enter a Detail"
    33.             Return False
    34.         End If
    35.         cell.ErrorText = Nothing
    36.         PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = Nothing
    37.         Return True
    38.     End Function
    39.  
    40.     Private Function IsPaymentMethodGood(ByRef cell As DataGridViewCell) As Boolean
    41.         If cell.Value.ToString().Length = 0 Then
    42.             cell.ErrorText = "Please enter a paymentMethod"
    43.             PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = "Please enter a paymentMethod"
    44.             Return False
    45.         End If
    46.         cell.ErrorText = Nothing
    47.         PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = Nothing
    48.         Return True
    49.     End Function
    50.  
    51.     Private Function IsPaymentAmountGood(ByRef cell As DataGridViewCell) As Boolean
    52.         If Not Integer.TryParse(cell.Value.ToString(), New Integer()) Then
    53.             cell.ErrorText = "Please enter a paymentAmount"
    54.             PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = "Please enter a paymentAmount"
    55.             Return False
    56.         End If
    57.         cell.ErrorText = Nothing
    58.         PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = Nothing
    59.         Return True
    60.     End Function
    61.  
    62.     Private Function IsGST_catGood(ByRef cell As DataGridViewCell) As Boolean
    63.         If cell.Value.ToString().Length = 0 Then
    64.             cell.ErrorText = "Please enter a GST_cat"
    65.             PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = "Please enter a GST_cat"
    66.             Return False
    67.         End If
    68.         cell.ErrorText = Nothing
    69.         PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = Nothing
    70.         Return True
    71.     End Function
    72.  
    73.     Private Function IsGSTAmountGood(ByRef cell As DataGridViewCell) As Boolean
    74.         If Not Integer.TryParse(cell.Value.ToString(), New Integer()) Then
    75.             cell.ErrorText = "Please enter a GSTAmount"
    76.             PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = "Please enter a GSTAmount"
    77.             Return False
    78.         End If
    79.         cell.ErrorText = Nothing
    80.         PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = Nothing
    81.         Return True
    82.     End Function
    83.  
    84.     Private Function IsDateGood(ByRef cell As DataGridViewCell) As Boolean
    85.         If cell.Value Is Nothing Then
    86.             cell.ErrorText = "Missing date"
    87.             PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = "Missing date"
    88.             Return False
    89.         Else
    90.             Try
    91.                 DateTime.Parse(cell.Value.ToString())
    92.             Catch ex As FormatException
    93.                 cell.ErrorText = "Invalid format"
    94.                 PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = "Invalid format"
    95.                 Return False
    96.             End Try
    97.         End If
    98.         cell.ErrorText = Nothing
    99.         PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = Nothing
    100.         Return True
    101.     End Function

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: DatagridView Validation

    Well, have you debugged? Have you stepped through the code to see why one of your cell validation functions returns False when it should apparently return True? Don't just look at the code. Watch it as it executes to see exactly what it does and why.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: DatagridView Validation

    i did debug but still couldnt work it out, but now i spotted this
    vb.net Code:
    1. Private Function IsPaymentAmountGood(ByRef cell As DataGridViewCell) As Boolean
    2.         If Not Integer.TryParse(cell.Value.ToString(), New Integer()) Then
    3.             cell.ErrorText = "Please enter a paymentAmount"
    4.             PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = "Please enter a paymentAmount"
    5.             Return False
    6.         End If
    7.         cell.ErrorText = Nothing
    8.         PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = Nothing
    9.         Return True
    10.     End Function

    which should have been this:
    vb.net Code:
    1. Private Function IsPaymentAmountGood(ByRef cell As DataGridViewCell) As Boolean
    2.         If Not Decimal.TryParse(cell.Value.ToString(), New Decimal()) Then
    3.             cell.ErrorText = "Please enter a paymentAmount"
    4.             PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = "Please enter a paymentAmount"
    5.             Return False
    6.         End If
    7.         cell.ErrorText = Nothing
    8.         PaymentsDataGridView.Rows(cell.RowIndex).ErrorText = Nothing
    9.         Return True
    10.     End Function

    thanks again

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: DatagridView Validation

    Quote Originally Posted by toecutter View Post
    i did debug but still couldnt work it out
    If you had debugged then you must have known exactly where the actual course of execution diverged from the expected course of execution and you must have known exactly what data was being used at the time. I don't see where you provided any of that information to us. Keeping relevant information secret from those whom you've asked to help you is not the best way to get the help you want. At best, it means that we have waste our time guessing and working things out that you already know.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: [RESOLVED] DatagridView Validation

    yes, sorry is not inteninal. information overload sometimes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width