Determining value of selected cell in datagrid


i need to do 2 things,

firstly,i have a datagrid with 5 columns. If user selects one or multiple cells/rows, i need to display the values in a message box when user clicks on button.

secondly, the entire row must be selected when user selects either a cell or row

please help....
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can use the DataGrid's HitTest method, passing it a point in the grid's client coordinate system, and returning a HitTestInfo object that holds all the row and column information.

    ' X and Y are in the grid's coordinates. If they are in
    ' screen coordinates, call DataGrid.PointToClient method.        
    Dim pt = New Point(X, Y)        
    Dim hti As DataGrid.HitTestInfo = Me.DataGrid.HitTest(pt)        

    If hti.Type = DataGrid.HitTestType.Cell Then        
        MessageBox.Show(DataGrid(hti.Row, hti.Column).ToString())        
        If hti.Type = DataGrid.HitTestType.ColumnHeader Then
          ' Assumes the DataSource is a DataView.        
          MessageBox.Show(CType(Me.DataGrid.DataSource,  _
        End If        
    End If       

to select the entire row use the method in the mouse up event

Private Sub dataGrid1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dataGrid1.MouseUp
     Dim pt = New Point(e.X, e.Y)
     Dim hti As DataGrid.HitTestInfo = dataGrid1.HitTest(pt)
     If hti.Type = DataGrid.HitTestType.Cell Then
          dataGrid1.CurrentCell = New DataGridCell(hti.Row, hti.Column)
     End If
End Sub
jerseybelleAuthor Commented:
hey checoo,

the second code worked..thanks!

but, still having problem with the first one....the message box is not displayed...and am not getting any error either...
am using this code in the button's on_click event...and am using a dataset for datagrid... the button is not a part of the datagrid..

please help!
Actually an easier way to get the values of the selected row will be to use

DataGrid1.CurrentRowIndex to get the index of the current selected row, and then fetch the valeus of the column from the underlying DataSet like

Dim dr as DataRow = myDataSet.Tables[myTableName].Rows(DataGrid1.CurrentRowIndex)

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.