Results 1 to 5 of 5

Thread: [RESOLVED] how to edit a cell and validate it in Datagrid view?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2012
    Posts
    22

    Resolved [RESOLVED] how to edit a cell and validate it in Datagrid view?

    Hi All,
    I am using a datagrid view.i want to edit the second column of the grid (as in the image below)and validate it ,to get data from database.Name:  invoice.jpg
Views: 1048
Size:  70.4 KB
    Which event of datagridview has to be used?I have used _cellbeginedit,_cellvalidating..but no use..
    Any help is appreciated.

  2. #2
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: how to edit a cell and validate it in Datagrid view?

    DataGridView1_CellValueChanged Occurs once you lose focus of the cell (click off). Note I show this event occuring when you click off the cell and provide a new value. You could then test e.columnindex and e.rowindex to make sure that you entered an item in the correct column index, then pass DataGridView1.Item(e.ColumnIndex, e.RowIndex).FormattedValue as a parameter to a query to get the info back.

    Code:
        Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
            Try
                If e.ColumnIndex = 1 Then
                    'do ado.net query here or call it on a background worker
                    MsgBox(DataGridView1.Item(e.ColumnIndex, e.RowIndex).FormattedValue) 'just to show the value
                End If
            Catch ex As Exception
                MessageBox.Show("Exception handling goes here")
            End Try
        End Sub
    Note what may be a better approach, you could also pull all the product codes and descriptions into a dictionary on load and then just fill them into the adjacent column using the event above. I suppose it depends how much data you need to sift through, your indexing, and how long you would like to wait..

    http://www.dotnetperls.com/dictionary-vbnet
    Last edited by jayinthe813; Sep 21st, 2013 at 12:27 AM.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2012
    Posts
    22

    Re: how to edit a cell and validate it in Datagrid view?

    Hi,Thankyou for your help.Now the thing is that,when the program is run,before the form is loaded,this event gets evoked,and an exception is shown.
    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

  4. #4
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: how to edit a cell and validate it in Datagrid view?

    Sorry, its getting late here, try:

    Code:
        Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
    
            If e.ColumnIndex = 1 And e.RowIndex <> -1 Then
                'do query here
                MsgBox(DataGridView1.Item(e.ColumnIndex, e.RowIndex).FormattedValue.ToString)
            End If
    
        End Sub

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2012
    Posts
    22

    Re: how to edit a cell and validate it in Datagrid view?

    Hi.Thanks.This one helped.. and resolved my problem.

Tags for this Thread

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