[2005] Validating DataGridView's cell
Hi,
How can I valide value in DataGridViews cell? I was trying to do it like that:
VB Code:
Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If Me.DataGridView1.CurrentCellAddress.X = 1 Then
If Not IsNumeric(Me.DataGridView1.CurrentCell.Value) Then
e.Cancel = True
MessageBox.Show("cell contains wrong value")
End If
End If
End Sub
But this code returns error regardless of value that was entered into cell.
Please help,
Regards,
sweet_dreams
Re: [2005] Validating DataGridView's cell
You should be using the properties of the 'e' argument, i.e. e.ColumnIndex and e.FormattedValue.
Re: [2005] Validating DataGridView's cell
Thanks jmcilhinney for your reply. But I have another problem: how can I avoid validating cell it isn't containing any value. I was trying to use such a code:
VB Code:
Private Sub dgvkryt_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgvkryt.CellValidating
Dim wartosc As Integer
If e.ColumnIndex = 6 Then
If IsDBNull(Me.dgvkryt.Rows(e.RowIndex).Cells("lp").Value) Then
Exit Sub
End If
If Me.dgvkryt.Rows(e.RowIndex).Cells("lp").Value = "" Then
Exit Sub
End If
If Not Integer.TryParse(e.FormattedValue.ToString, wartosc) _
Or wartosc < 0 Then
e.Cancel = True
MessageBox.Show("wrong value", "error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End If
End Sub
I was trying to check value using:
VB Code:
If IsDBNull(Me.dgvkryt.Rows(e.RowIndex).Cells("lp").Value) Then
Exit Sub
End If
If Me.dgvkryt.Rows(e.RowIndex).Cells("lp").Value = "" Then
Exit Sub
End If
Unfortuantely when I insert value to cell and I go to another cell, previous cell is not validating. Only when I come back to this cell. Whats more I would like to stop validating when I delete content of cell.
Please help,
Regards,
sweet_dreams