Determining value of selected cell in datagrid

Posted on 2005-04-26
Last Modified: 2010-05-18

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....
Question by:jerseybelle
    1 Comment
    LVL 9

    Accepted 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 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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now