Results 1 to 3 of 3

Thread: [2005] Validating DataGridView's cell

  1. #1

    Thread Starter
    Addicted Member sweet_dreams's Avatar
    Join Date
    Apr 2005
    Location
    Poland, Lodz
    Posts
    189

    Question [2005] Validating DataGridView's cell

    Hi,

    How can I valide value in DataGridViews cell? I was trying to do it like that:

    VB Code:
    1. Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
    2.    
    3.         If Me.DataGridView1.CurrentCellAddress.X = 1 Then
    4.             If Not IsNumeric(Me.DataGridView1.CurrentCell.Value) Then
    5.                 e.Cancel = True
    6.                 MessageBox.Show("cell contains wrong value")
    7.             End If
    8.         End If
    9.     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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Validating DataGridView's cell

    You should be using the properties of the 'e' argument, i.e. e.ColumnIndex and e.FormattedValue.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member sweet_dreams's Avatar
    Join Date
    Apr 2005
    Location
    Poland, Lodz
    Posts
    189

    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:
    1. Private Sub dgvkryt_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgvkryt.CellValidating
    2.         Dim wartosc As Integer
    3.  
    4.         If e.ColumnIndex = 6 Then
    5.  
    6.             If IsDBNull(Me.dgvkryt.Rows(e.RowIndex).Cells("lp").Value) Then
    7.                 Exit Sub
    8.             End If
    9.  
    10.             If Me.dgvkryt.Rows(e.RowIndex).Cells("lp").Value = "" Then
    11.                 Exit Sub
    12.             End If
    13.  
    14.             If Not Integer.TryParse(e.FormattedValue.ToString, wartosc) _
    15.             Or wartosc < 0 Then
    16.                 e.Cancel = True
    17.                 MessageBox.Show("wrong value", "error", _
    18.                  MessageBoxButtons.OK, MessageBoxIcon.Error)
    19.             End If
    20.         End If
    21.  
    22.     End Sub

    I was trying to check value using:
    VB Code:
    1. If IsDBNull(Me.dgvkryt.Rows(e.RowIndex).Cells("lp").Value) Then
    2.                 Exit Sub
    3.             End If
    4.  
    5.             If Me.dgvkryt.Rows(e.RowIndex).Cells("lp").Value = "" Then
    6.                 Exit Sub
    7.             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
  •  



Click Here to Expand Forum to Full Width