6 Replies - 368 Views - Last Post: 25 January 2013 - 05:18 PM Rate Topic: -----

#1 ElectroGrind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-April 10

DataGrid Input Validation

Posted 25 January 2013 - 02:55 PM

I am having trouble getting an error message to appear when a user inputs invalid information into a cell into the datagridview.

 Private Sub btnUpdate_Click(sender As System.Object, e As System.EventArgs) Handles btnUpdate.Click
        'Saves and updates any changes made to the database
        Me.Validate()
        If (Me.CustomerTableAdapter.Fill(Me.Aurora2DataSet.Tables("Customer"))) Then

            DataGridView1.DataSource = Me.Aurora2DataSet.Tables("Customer")

            MessageBox.Show("New Customer Added")
        Else
            MessageBox.Show("Invalid Input")
        End If
        Me.Aurora2DataSet.AcceptChanges()


When I inserted break points I noticed that even though I inputted invalid information it would display the first message box and skip over the else statement and move down to the end if. Even though it displays the wrong message box none of the changes are committed back to the database.

My question is what would be causing it to display the first messagebox and skip over the else statement when invalid information is submitted?

Is This A Good Question/Topic? 0
  • +

Replies To: DataGrid Input Validation

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3175
  • View blog
  • Posts: 10,625
  • Joined: 12-December 12

Re: DataGrid Input Validation

Posted 25 January 2013 - 03:56 PM

The first thing that strikes me is that you are calling Me.Validate() but you are not checking the return result (a boolean):

If Me.Validate() Then
    'submit data
Else
    'do something else
End If


BTW The Fill method, I understand, returns an Integer value, so you should check this number, rather than assuming a boolean return value. Alternatively, and if appropriate, you might check .Rows.Count.

This post has been edited by andrewsw: 25 January 2013 - 04:05 PM

Was This Post Helpful? 0
  • +
  • -

#3 ElectroGrind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-April 10

Re: DataGrid Input Validation

Posted 25 January 2013 - 04:19 PM

I tried this but I'm still getting the same result.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3175
  • View blog
  • Posts: 10,625
  • Joined: 12-December 12

Re: DataGrid Input Validation

Posted 25 January 2013 - 04:35 PM

View PostElectroGrind, on 25 January 2013 - 04:19 PM, said:

I tried this but I'm still getting the same result.

Are your Validation events triggered and do they work as expected? You need to step through your code - do some basic debugging.

[Shouldn't the Validation events be producing the error message(s)?]

This post has been edited by andrewsw: 25 January 2013 - 04:48 PM

Was This Post Helpful? 0
  • +
  • -

#5 ElectroGrind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-April 10

Re: DataGrid Input Validation

Posted 25 January 2013 - 04:49 PM

From what I can tell the validation is being triggered but once it hits the first messagebox it displays it and skips over the rest. Is there any way to check to see if its getting validated against information held within the database?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3175
  • View blog
  • Posts: 10,625
  • Joined: 12-December 12

Re: DataGrid Input Validation

Posted 25 January 2013 - 05:12 PM

How are you performing validation against the database? You need to show some code.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3175
  • View blog
  • Posts: 10,625
  • Joined: 12-December 12

Re: DataGrid Input Validation

Posted 25 January 2013 - 05:18 PM

If you are validating against the database then it is better to use the CellValidating event rather than relying on Form validation. (You hadn't mentioned previously that you were validating against the database.)

Walkthrough: Validating Data in the Windows Forms DataGridView Control

Added: Mmm have you actually coded any validation?! It won't happen of its own accord.

This post has been edited by andrewsw: 25 January 2013 - 05:29 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1