Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataError Questions

Status
Not open for further replies.

kimprogrammer

Programmer
Sep 15, 2008
160
CA
Hello
I have created a datagrid I've only choosen to display 4 of the columns from my database. They are PPstartDate, PayCode,Amount and Comments. The rest of the fields I will fill in when I go and update the database.

I have the following code(see below) and when I enter a line I get the following error from my messagebox "another error", when I display e.column I get 3 and e.context I get Commit.

So it looks like its telling me that there is problem with the comments column(this the user can enter or leave blank).

But when I comment out the code and let the datagrid error dialog box show the error it says Column EmpNum does not allow nulls. Which is a field on the database table but not displayed in the datagrid view

So I though I understood that e.columnIndex corresponded to the columns I'm displaying because the first three columns display corresponding indexes.

Could someone explain this to me.

Code;
------------------------------------
Private Sub SwipeAccumulativeHoursDataGridView_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles SwipeAccumulativeHoursDataGridView.DataError
' Select Case e.ColumnIndex
' Case 1 'needs paycode
' Debug.WriteLine("e.ColumnIndex")
' Debug.WriteLine(e.ColumnIndex)
' MessageBox.Show("Must enter Pay Code", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
' Case 0 'Needs to be a datetime
' Debug.WriteLine("e.ColumnIndex")
' Debug.WriteLine(e.ColumnIndex)

' MessageBox.Show("Date - Must be a valid date", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
' Case 2 'Needs to be a decimal
' Debug.WriteLine("e.columnindex")
' Debug.WriteLine(e.ColumnIndex)
' MessageBox.Show("Amount - Must enter a numeric field", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
' Case Else
' Debug.WriteLine("e.ColumnIndex")
' Debug.WriteLine(e.ColumnIndex)
' Debug.WriteLine(e.Context)
' MessageBox.Show("another error", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
' End Select

'End Sub
-----------------------------------------------
 
Here's the thing--
Your database table(s) has a schema with defined columns, types, constraints, etc. In many cases, you also bring those rules into your DataSet in your .Net application, which is what I think you are doing.

So when you add a new row to the DataTable with your DataGridView in .Net, if there is no value entered for your column which does not allow nulls, you're going to get an error. It doesn't realize that you're going to update those values manually. One thing to try, although I'm not sure I like the idea too much is to set .AllowDBNull to true on your DataColumn.

The reason why it's showing the incorrect column index is because it's validating the column your are going off of--it gets an error but you were on a different column. Maybe in your case, it is better to try MessageBox.Show(ex.Exception.Message) instead of using the case statement.
 
I'm familiar with MessageBox.Show(ex.Exception.Message) when it is around a try/catch because you define the ex. But if I have messagebox.show in the DataGridView_DataError what am I asking as part of the try?

I guess the columnindex does not quite work as I was understanding it so it might not work with the other things I need to do. I need a bit more validation on a couple of the fields - which I was experimenting with cellenter and cellclick but don't think it works like I'd like. I also am totaling the different paycodes and displaying them under the datagrid which works ok when I first load the data but then want to add in the numbers each time the user enters a new row.

Not one of the books I have talk anything about datagrid validation or e.columnindex or e.rowindex to help me get an understanding of this.
 
(Should have been e.Exception.Message). The way it seems to work is that the DataError event is fired for the cell you were on when the row is being added to the underlying datasource for the "does not allow nulls" constraint. For datatype validation issues, it appears to fire when the cell is being validated.
 
I just can't believe I'm having so much trouble trying to get simple data validation.

Below is my code now. When I enter a day I still get a need to enter date error. I still get the error about empnum so I'm not sure what to do there. And it seems like the errors messages pop up when I have valid data in them.

Private Sub SwipeAccumulativeHoursDataGridView_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles SwipeAccumulativeHoursDataGridView.CellValidating
Select Case e.ColumnIndex
Case 0
If SwipeAccumulativeHoursDataGridView.CurrentRow.Cells(e.ColumnIndex).Value Is DBNull.Value Then
MessageBox.Show("You need to enter a date", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
If Date.Parse(SwipeAccumulativeHoursDataGridView.CurrentRow.Cells(e.ColumnIndex).Value) < StartDate And _
Date.Parse(SwipeAccumulativeHoursDataGridView.CurrentRow.Cells(e.ColumnIndex).Value) > EndDate Then
MessageBox.Show("Date not within payperiod", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End If

Case 1
If SwipeAccumulativeHoursDataGridView.CurrentRow.Cells(e.ColumnIndex).Value Is DBNull.Value Then
MessageBox.Show("You need to enter a paycode", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If

Case 3
If SwipeAccumulativeHoursDataGridView.CurrentRow.Cells(e.ColumnIndex).Value Is DBNull.Value Then
MessageBox.Show("You need to enter an amount", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
If Not IsNumeric(SwipeAccumulativeHoursDataGridView.CurrentRow.Cells(e.ColumnIndex).Value) Then
MessageBox.Show("You need to enter a numeric", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End If


'total the paycodes
ZeroTotals()
TotalPaycodes()
PrintTotals()
Case Else


End Select
End Sub
 
I just can't believe

I can.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top