• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

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....
1 Solution
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 datagrid.select 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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now