Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning the contents of row when DataGrid is clicked

Status
Not open for further replies.

dpdoug

Programmer
Nov 27, 2002
455
US
I have a DataGrid on a Winform and I need to get the cell contents from a certain row when it is clicked.

The only event that I get when I double click the datagrid in design view is DataGrid1_Navigate, which doesn't do what I want, I don't think.

So how do I return the cell contents of a row when it is clicked?
 
The only event that I get when I double click the datagrid in design view is DataGrid1_Navigate
No, that's the only event that gets written out for you. Have you tried selecting any of the other events associated with this control?

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Assuming the DataGrig is bound to a datasource, use a CurrencyManager and a DtatRowView:

Dim cm As CurrencyManager
Dim drv As DataRowView

cm = DataGrid1.BindingContext(<data_source_object>)

where <data_source_object> is the DataSource of the DataGrid (e.g., DataTable, DataView, etc.)

Next set up this code to register a click on the grid. I use the MouseUp event, but you could also put this code in the DoubleClick event:

Private Sub DgMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseUp
Dim hit As System.Windows.Forms.DataGrid.HitTestInfo

hit = sender.HitTest(e.X, e.Y)

If hit.Type = Windows.Forms.DataGrid.HitTestType.Cell Or hit.Column = -1 Then
'Dont execute code if clicked on Header row
If hit.Row = -1 Then Exit Sub

'select the entire row (comment out if this is not what you want)
sender.select(hit)

'get selected row into the DataRowView
drv = CType(cm.Current, DataRowView)

End Sub

You can now reference any of the DataGrid's cell contents for the row selected by using the DataRowView's .Item property:

SomeData = drv.Item("SomeFieldName")

or

SomeData = drv.Item(2)

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top