[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Determining value of selected cell in datagrid

Posted on 2005-04-26
Medium Priority
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
  • 2

Expert Comment

ID: 13873017
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

Author Comment

ID: 13876298
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!

Accepted Solution

checoo earned 1500 total points
ID: 13882547
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

872 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