Data grid view row validating problems

syntia.wijaya

Member
Joined
Apr 22, 2007
Messages
24
Location
Indonesia
Programming Experience
Beginner
Dear experts,

I have a datagridview that gives me never ending problems. All the columns in datagridview is DataGridViewTextBoxColumn. I also have 3 buttons: "Add", "Edit" and "Delete".

In my database, I set not null for several columns.

This is the code in my add button click event
VB.NET:
MyBindingSource.AddNew()[/COLOR]
[COLOR=black]AddButton.Text = "Save"[/COLOR]
[COLOR=black]EditButton.Text = "Cancel"[/COLOR]

After pressing the "Add" button, a new row occured. If I havent entered all the mandatory columns and change row, this error pop up.[/font]

See image error.jpg

With my very limited knowledge, I thought it's because the row validating event is fired. Well, I do need the error because I don't want user to change row before finish entering all the mandatory column.

I find something in MSDN and try to use it in my code: (FYI this sub handles RowValidating event, which is inherited from mother's form)

VB.NET:
[/COLOR]
[COLOR=black]Protected Overrides Sub DataGridView1_RowValidating(ByVal sender As Object, ByVal Data As System.Windows.Forms.DataGridViewCellCancelEventArgs)[/COLOR]
[COLOR=black]    If temp = "Add" Or temp = "Edit" Then[/COLOR]
[COLOR=black]        Dim row As DataGridViewRow = DataGridView1.Rows(Data.RowIndex)[/COLOR]
[COLOR=black]        Dim projectIDCell As DataGridViewCell = row.Cells(DataGridView1.Columns("ProjectIDColumn").Index)[/COLOR]
[COLOR=black]        Dim projectNameCell As DataGridViewCell = row.Cells(DataGridView1.Columns("ProjectNameColumn").Index)[/COLOR]
[COLOR=black]        Dim planStartDateCell As DataGridViewCell = row.Cells(DataGridView1.Columns("PlanStartDateColumn").Index)[/COLOR]
[COLOR=black]        Dim planFinishDateCell As DataGridViewCell = row.Cells(DataGridView1.Columns("PlanFinishDateColumn").Index)[/COLOR]
[COLOR=black]        Dim clientCell As DataGridViewCell = row.Cells(DataGridView1.Columns("ClientColumn").Index)[/COLOR]
[COLOR=black]        Data.Cancel = Not (IsProjectIDGood(projectIDCell) AndAlso IsProjectNameGood(projectNameCell) AndAlso IsPlanStartDateGood(planStartDateCell) AndAlso IsPlanFinishDateGood(planFinishDateCell) AndAlso IsClientGood(clientCell))[/COLOR]
[COLOR=black]    End If[/COLOR]
[COLOR=black]End Sub[/COLOR]

This inside the IsProjectIDGood, IsProjectNameGood, IsClientGood Function, column that have string value
VB.NET:
Private Function IsProjectIDGood(ByRef cell As DataGridViewCell) As Boolean[/COLOR]
[COLOR=black]    If cell.Value.ToString().Length = 0 Then[/COLOR]
[COLOR=black]        cell.ErrorText = "Please enter Project ID"[/COLOR]
[COLOR=black]        DataGridView1.Rows(cell.RowIndex).ErrorText = "Please enter Project ID"[/COLOR]
[COLOR=black]        Return False[/COLOR]
[COLOR=black]    End If[/COLOR]
[COLOR=black]    Return True[/COLOR]
[COLOR=black]End Function[/COLOR]

This is for date column.
VB.NET:
[COLOR=black]Private Function IsPlanStartDateGood(ByRef cell As DataGridViewCell) As Boolean[/COLOR]
[COLOR=black]    If cell.Value Is Nothing Then[/COLOR]
[COLOR=black]        cell.ErrorText = "Missing Plan Start Date"[/COLOR]
[COLOR=black]        DataGridView1.Rows(cell.RowIndex).ErrorText = "Missing Plan Start Date"[/COLOR]
[COLOR=black]        Return False[/COLOR]
[COLOR=black]    Else[/COLOR]
[COLOR=black]        Try[/COLOR]
[COLOR=black]            DateTime.Parse(cell.Value.ToString())[/COLOR]
[COLOR=black]        Catch ex As FormatException[/COLOR]
[COLOR=black]            cell.ErrorText = "Invalid format"[/COLOR]
[COLOR=black]            DataGridView1.Rows(cell.RowIndex).ErrorText = "Invalid format"[/COLOR]
[COLOR=black]            Return False[/COLOR]
[COLOR=black]        End Try[/COLOR]
[COLOR=black]    End If[/COLOR]
[COLOR=black]    Return True[/COLOR]
[COLOR=black]End Function[/COLOR]

CMIIW, the code for the date column spose to handle error if i entered let's say letters or wrong date format ("1/3432534634"), but it doesn't. It gives me this popup below, even when I press "Save" button, "Cancel" button, even when i pressed that cross icon at the top right side of the form. I have to press "stop debugging" icon in VS to terminate the application. FYI, when the "Cancel" button is pressed, the just added row sposed to be deleted, but it did noting when I pressed it.

See image error2.jpg

So my question is, how do i prevent the error from popping up but keep validating the row and give ErrorText instead.

Sorry for the long post..

Thank you
 

Attachments

  • error.JPG
    error.JPG
    51.8 KB · Views: 24
  • error2.JPG
    error2.JPG
    77.7 KB · Views: 23
Last edited:
Dear cjard,

Thank you for your reply..

I tried the CalendarColumn class and change the code according to this http://www.vbdotnetforums.com/showthread.php?t=19461 (while i'm not sure what it is for).

I got another problem. This is what I do when the add button is clicked:
VB.NET:
tm_ProjectBindingSource.AddNew()

So, all the value in new row is empty. When I click the column date, I got InvalidCastException pointing particular code, as shown in the picture. I thought because the value that is casted is empty (CMIIW). So I put default value when the add button is clicked:
VB.NET:
tm_ProjectBindingSource.AddNew()
Me.DataGridView1.Rows(Me.DataGridView1.CurrentRow.Index).Cells(4).Value = Date.Now()
Me.DataGridView1.Rows(Me.DataGridView1.CurrentRow.Index).Cells(5).Value = Date.Now()

This way, it works well, just one thing. When I save the record, the saved date is Date.Now(), not the one that I enter. How do I solve this?

Thank you..
 

Attachments

  • error.JPG
    error.JPG
    83.1 KB · Views: 36
So, all the value in new row is empty. When I click the column date, I got InvalidCastException pointing particular code, as shown in the picture. I thought because the value that is casted is empty (CMIIW). So I put default value when the add button is clicked:

I wouldnt set the default value that way. Instead, put a line of code just before you call AddNew(), saying:

<your dataset name>.tm_Project.<your date column name>.DefaultValue = DateTime.Now()


You will have to replace the bold bits with the right things
 
alternately you can edit the code that crashes and check if it is null first, if it is, initialize the conttrol with datetime.now - this might not be a better option than the first though.
 
Back
Top