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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to get the value that a user selects in a DataGrid

Status
Not open for further replies.

jakeir

Programmer
Apr 7, 2009
25
US
Hi,

I used to do this but have not been able to find it a again, anywhere on the net. I am trying to get the value of a cell that a user selcets in a DataGrid. For example, If I have in cell one IDs and customers Name in cell2 and the person selects cell1 (any row) they will get the value in that cell. And I am using VB6.

Any ideas would be really appreciated.

Thank you
 
I usually use the msflexgrid and it has a property called textmatrix(x,y) in which you can determine any value. Perhaps the datagrid has a similar property.

Eg.
flgGrid.TextMatrix(flgGrid.Row, 0)

If at first you don't succeed, then sky diving wasn't meant for you!
 
If you use the MouseUp or MouseDown events for a datagrid, those events provide a X (horizontal) and Y (vertical) screen coordinate. You can then use the RowContaining(Y) and ColContaining(X) datagrid properties to retrieve the row and column that was clicked.

Just remember to trap errors when using these because the functions can return an error if the user clicks inside the grid but not a specific row and column.
 
Thanks for your help but unfortuantly there is nothing like that for a datagrid that I can see. I know how to do this for a flex grid but I wanted to see how to do it for a Datagrid. If you or anyone has any further ideas I would appreciat it.
Thank you
 
To Golom: thnaks this came close but it just retuns a number, and I am not sure the number is. But I am looking that if the cell had the name Bill in it, it would pull htis out and I can assigin it to a variable or a textbox. I have done this with a flexgrid but wanted toi do it with a datagrid. Do you know what I am doing wrong, that using your method I am only geting a number?
Thank you
 
The numbers returned by RowContaining and ColContaining are respectively the row and column numbers in which the cursor is positioned. To get the value in that row and column
Code:
Private Sub myGrid_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
    Dim CellValue As Variant
    On Error GoTo IgnoreIt
    With myGrid
        .Row = .RowContaining(Y)
        CellValue = .Columns(.ColContaining(X)).Value
    End With
    [COLOR=black cyan]' Now do something with the CellValue[/color]
    Exit Sub
IgnoreIt:
    [COLOR=black cyan]' Error 6148 is an invalid row or column number resulting from 'RowContaining(Y) or ColContaining(X)'[/color]
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top