1 Attachment(s)
[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.Attachment 104921
Which event of datagridview has to be used?I have used _cellbeginedit,_cellvalidating..but no use..
Any help is appreciated.
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
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
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
Re: how to edit a cell and validate it in Datagrid view?
Hi.Thanks.This one helped.. and resolved my problem.