|
-
Sep 21st, 2013, 12:02 AM
#1
Thread Starter
Junior Member
[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.
Which event of datagridview has to be used?I have used _cellbeginedit,_cellvalidating..but no use..
Any help is appreciated.
-
Sep 21st, 2013, 12:18 AM
#2
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.
-
Sep 21st, 2013, 12:37 AM
#3
Thread Starter
Junior Member
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
-
Sep 21st, 2013, 12:41 AM
#4
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
-
Sep 21st, 2013, 12:44 AM
#5
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|