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
    LVL 9

    Expert Comment

    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

    Author Comment

    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!
    LVL 9

    Accepted Solution

    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)

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    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.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    745 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

    14 Experts available now in Live!

    Get 1:1 Help Now