syntia.wijaya
Member
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
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)
This inside the IsProjectIDGood, IsProjectNameGood, IsClientGood Function, column that have string value
This is for date column.
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
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
Last edited: