Join 95,475 VB.NET Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 956 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
From what I'm finding, there doesn't appear to be any particular function available to obtain the value from a particular field from a double clicked on row of a datagrid...
So if you wanted to double click on a particular result in a datagrid, then pass one or more of the values of that row, you couldn't?
I was thinking something like this, but of course that doesn't work, any ideas?
CODE
Private Sub InventoryDataGridView_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles InventoryDataGridView.CellContentDoubleClick Dim invID As String = InventoryDataGridView.CurrentRow(???ParticularField???) End Sub
First time for me to use this control in vb.net - lol. Anyhow i guess you could use something like this:
CODE
Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick If e.ColumnIndex > -1 AndAlso e.RowIndex > -1 AndAlso TypeOf sender.CurrentCell Is DataGridViewTextBoxCell Then 'AndAlso TypeOf sender.CurrentCell Is DataGridViewTextBoxCell MsgBox("Value= " & sender.CurrentCell.EditedFormattedValue()) End If End Sub
'this is just to show how event works; you do not need to use it Private Sub DataGridView1_CellMouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellMouseEnter Me.Text = "No way: column = " & e.ColumnIndex & " and row=" & e.RowIndex End Sub
I am not curtain if one needs to use the preliminary check - if one double clicked item and not the row or column selector, but i think using the type check could be advised.
--------------------
Then anyone who leaves behind him a written manual, and likewise anyone who receives it, in the belief that such writing will be clear and certain, must be exceedingly simple-minded. (Plato, Phaedrus)
Alright, came up with this, and it gives me the value of the cell in which was clicked, which is definately a step in the correct direction...
CODE
Private Sub InventoryDataGridView_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles InventoryDataGridView.CellDoubleClick Dim invId As Object = InventoryDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value inventoryIdLabel.Text = Convert.ToString(invId) End Sub
The only real problem now is that you can't depend on a user to click on the id field of the row, so you would need to obtain the id field of the row from what ever field was clicked in that row... any ideas?
The rows and cells property of the datagridview takes an integer index as reference to a specific row or column in the grid. So determine which column is the one containing the ID field and replace e.ColumnIndex with that column number.
Don't forget the column and row numbers start from 0.
For example, if the ID column is the first column in the grid:
CODE
Dim invId As Object = InventoryDataGridView.Rows(e.RowIndex).Cells(0).Value
Works like a charm, thank you very much... I think I pretty much have what I need now to complete my final project...
I can now select, insert, update, & delete sql with vb .net, I can pass values from a datagrid to another form, LOL, albeit I'm sure not in the manner in which an experienced coder would do it, but it works... I write the id I get from the datagrid to a text file, then on the load of the new form I pull that id from the text file and run the query...
Just wanted to thank you all again for your continued assistance.
Const index_of_id_row As Int32 = 762 Private Sub DataGridView1_CellContentClick( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs _ ) Handles DataGridView1.CellMouseDoubleClick
If TypeOf sender.CurrentCell Is DataGridViewTextBoxCell Then Call SetValueToMyTextBox(CType(sender, DataGridView), e) End If End Sub Private Sub SetValueToMyTextBox( _ ByVal sender As DataGridView, _ ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs _ ) If sender.CurrentRow.Index = index_of_id_row Then inventoryIdLabel.Text = sender.CurrentCell.EditedFormattedValue() End If End Sub
would work.
Added: Did not see the last post, when i posted this. Guess it's not bad because it also should show how to get rid of "late binding error".
This post has been edited by m2s87: 28 Aug, 2007 - 02:15 PM
--------------------
Then anyone who leaves behind him a written manual, and likewise anyone who receives it, in the belief that such writing will be clear and certain, must be exceedingly simple-minded. (Plato, Phaedrus)
Hi u can try this: for example u want Id Field on click of datagrid then try this i think this should work .
Dim invID AsInteger= DataGridView1.CurrentRow.Cells(<columnIndex of particular colum eg. 0 ,1 etc>).value
All d best.
QUOTE(webwired @ 28 Aug, 2007 - 06:09 AM)
From what I'm finding, there doesn't appear to be any particular function available to obtain the value from a particular field from a double clicked on row of a datagrid...
So if you wanted to double click on a particular result in a datagrid, then pass one or more of the values of that row, you couldn't?
I was thinking something like this, but of course that doesn't work, any ideas?
CODE
Private Sub InventoryDataGridView_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles InventoryDataGridView.CellContentDoubleClick Dim invID As String = InventoryDataGridView.CurrentRow(???ParticularField???) End Sub