maxgall
asked on
cell validating in datagridview
I am struggling to validate cells in datagridview view.
I have created a very simple vb application in vb studio 2005.
created datasourse, databinding etc. Dragged and dropped a datagridview on my form including the little toolstrip bar with
with add new row, delete and save buttons.
When I click add new row it creates a new row which is empty. If I fill in the two columns and click save it works.
If I forget to input a value it obviously gives me a vb empyt string error. Can you please show me a small example of how a validation would work. I tried the one from MSDE but could not get it to work. Here is all the code I have in my application>
************************** ********** ********** ********** ********** ****
Public Class Form1
Private Sub PupilRecordsBindingNavigat orSaveItem _Click(ByV al sender As System.Object, ByVal e As System.EventArgs) Handles PupilRecordsBindingNavigat orSaveItem .Click
Me.Validate()
Me.PupilRecordsBindingSour ce.EndEdit ()
Me.PupilRecordsTableAdapte r.Update(M e.SchoolDa taSet.pupi lRecords)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'SchoolDataSet.pupilRecord s' table. You can move, or remove it, as needed.
Me.PupilRecordsTableAdapte r.Fill(Me. SchoolData Set.pupilR ecords)
End Sub
Private Sub BindingNavigatorDeleteItem _Click(ByV al sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem .Click
End Sub
Private Sub PupilRecordsDataGridView_C ellEndEdit (ByVal sender As Object, ByVal e As System.Windows.Forms.DataG ridViewCel lEventArgs ) Handles PupilRecordsDataGridView.C ellEndEdit
PupilRecordsDataGridView.R ows(e.RowI ndex).Erro rText = String.Empty
End Sub
Private Sub PupilRecordsDataGridView_C ellValidat ing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataG ridViewCel lValidatin gEventArgs ) Handles PupilRecordsDataGridView.C ellValidat ing
If PupilRecordsDataGridView.C olumns(e.C olumnIndex ).Name = "Name" Then
If String.IsNullOrEmpty(e.For mattedValu e.ToString ()) Then
PupilRecordsDataGridView.R ows(e.RowI ndex).Erro rText = "Empty"
e.Cancel = True
End If
End If
End Sub
End Class
************************** ********** ********** ********** ****
I might have the code in the wrong place because nothing happens with that.
thanks
Max
I have created a very simple vb application in vb studio 2005.
created datasourse, databinding etc. Dragged and dropped a datagridview on my form including the little toolstrip bar with
with add new row, delete and save buttons.
When I click add new row it creates a new row which is empty. If I fill in the two columns and click save it works.
If I forget to input a value it obviously gives me a vb empyt string error. Can you please show me a small example of how a validation would work. I tried the one from MSDE but could not get it to work. Here is all the code I have in my application>
**************************
Public Class Form1
Private Sub PupilRecordsBindingNavigat
Me.Validate()
Me.PupilRecordsBindingSour
Me.PupilRecordsTableAdapte
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'SchoolDataSet.pupilRecord
Me.PupilRecordsTableAdapte
End Sub
Private Sub BindingNavigatorDeleteItem
End Sub
Private Sub PupilRecordsDataGridView_C
PupilRecordsDataGridView.R
End Sub
Private Sub PupilRecordsDataGridView_C
If PupilRecordsDataGridView.C
If String.IsNullOrEmpty(e.For
PupilRecordsDataGridView.R
e.Cancel = True
End If
End If
End Sub
End Class
**************************
I might have the code in the wrong place because nothing happens with that.
thanks
Max
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks vm I can now validate the cells but the problem I have is when you add entry button a new row is created with a new ID Number generated. When clicking the add new entry button again a new row is created and the previous row not validated and when clicking on the save button a generic error about empty fields is created. Any Idea how I can prevent a new row beeing created when the previous one is still empty?
Interesting, shouldn't be so difficult, but I cannot think of something at the moment, and Im quite busy, so I would suggest you to add a new question, so someone else can have a look?
ASKER
Ok Thanks
ASKER