vb.net Datagrid row selection problem

lauriemc

Freshman
Joined
Feb 2, 2007
Messages
26
Hello, I am having a problem pulling data from a row Double-clicked in a vb data grid. What I want to do is double-click the row, put the first column's contents in one text box, the second column's contents in a second text box. Unfortunately the row index I am getting back is always -1.

I have tried:

Dim pt = New Point(e.X, e.Y)
Dim hit As DataGrid.HitTestInfo = Me.dgExtract.HitTest(pt)
rowIndex = hit.Row

But in debug mode, when I hover the mouse over rowIndex, it is alway -1.

I have tried this:

Dim rowIndex As Integer
rowIndex = Me.dgExtract.CurrentRowIndex

Me.txt_BatchNumber.Text = Me.dgExtract.Item(rowIndex, 0).ToString
Me.txt_StoreID.Text = Me.dgExtract.Item(rowIndex, 1).ToString

Again, the rowIndex is -1, if I let it go, it errors out on me when it tries to assign the value.

I found the above code in this forum. It seems logical, am I missing something simple ?

lauriemc :confused:
 
This stuff may or may not help you. I havent tested it out myself. Sorry if it doesnt. Also, do you mean datagrid control or datagridview control? This stuff is for datagridview controls (sorry never used datagrid control)..

1. I thought when extracting values from a cell you'd want to use the .Value property. So that Me.txt_BatchNumber.Text = Me.dgExtract.Item(rowIndex, 0).VALUE.ToString

2. You might want to use a Cell-Double Click event. Then when that event triggers the 'e' will provide you with the rowindex and the columnindex.

nevermind, i think you're using a Datagrid control. I'll post this anyway in case you change to a datagridview control
 
I found the problem, and it was a pretty silly one. I had two grids on my form and I was referencing the wrong grid - of course it was always -1. Am posting this embaressment just in case it can help someone else spot such an error....
 
Back
Top