|
-
Aug 29th, 2006, 06:00 AM
#1
Thread Starter
Addicted Member
[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
Last edited by sweet_dreams; Aug 29th, 2006 at 12:44 PM.
using VB 2010 .NET Framework 4.0; MS Office 2010; SQL Server 2008 R2 Express Edition | Remember to mark resolved threads and rate useful posts. 
-
Aug 29th, 2006, 06:39 AM
#2
Re: [2005] Validating DataGridView's cell
You should be using the properties of the 'e' argument, i.e. e.ColumnIndex and e.FormattedValue.
-
Aug 29th, 2006, 12:56 PM
#3
Thread Starter
Addicted Member
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
using VB 2010 .NET Framework 4.0; MS Office 2010; SQL Server 2008 R2 Express Edition | Remember to mark resolved threads and rate useful posts. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|