Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Using a column in a selected row - DataGrid

Using a column in a selected row - DataGrid

Scheduled Pinned Locked Moved Visual Basic
questionhelp
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    ml3bf
    wrote on last edited by
    #1

    Hi, I am trying to select a row in a DataGrid by simply clicking on the row (any column in the row) which will in turn open a new form with various controls which are binded to the original record which was selected in the DataGrid. I have the following code which selects the whole row when clicked Private Sub DataGridComplaints_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridComplaints.MouseUp Dim pt = New Point(e.X, e.Y) Dim hti As DataGrid.HitTestInfo = DataGridComplaints.HitTest(pt) If hti.Type = DataGrid.HitTestType.Cell Then DataGridComplaints.CurrentCell = New DataGridCell(hti.Row, hti.Column) DataGridComplaints.Select(hti.Row) End If End Sub How do I use a column in the selected row to pass information into a variable which can then be used on the next form? Many thanks for any help you can give me.

    C P 2 Replies Last reply
    0
    • M ml3bf

      Hi, I am trying to select a row in a DataGrid by simply clicking on the row (any column in the row) which will in turn open a new form with various controls which are binded to the original record which was selected in the DataGrid. I have the following code which selects the whole row when clicked Private Sub DataGridComplaints_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridComplaints.MouseUp Dim pt = New Point(e.X, e.Y) Dim hti As DataGrid.HitTestInfo = DataGridComplaints.HitTest(pt) If hti.Type = DataGrid.HitTestType.Cell Then DataGridComplaints.CurrentCell = New DataGridCell(hti.Row, hti.Column) DataGridComplaints.Select(hti.Row) End If End Sub How do I use a column in the selected row to pass information into a variable which can then be used on the next form? Many thanks for any help you can give me.

      C Offline
      C Offline
      cosminadrianpopescu
      wrote on last edited by
      #2

      try this: var=DataGridComplaints.item(hti.Row,x) 'where x is the number of the column that you are trying to get data from Cosmin

      1 Reply Last reply
      0
      • M ml3bf

        Hi, I am trying to select a row in a DataGrid by simply clicking on the row (any column in the row) which will in turn open a new form with various controls which are binded to the original record which was selected in the DataGrid. I have the following code which selects the whole row when clicked Private Sub DataGridComplaints_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridComplaints.MouseUp Dim pt = New Point(e.X, e.Y) Dim hti As DataGrid.HitTestInfo = DataGridComplaints.HitTest(pt) If hti.Type = DataGrid.HitTestType.Cell Then DataGridComplaints.CurrentCell = New DataGridCell(hti.Row, hti.Column) DataGridComplaints.Select(hti.Row) End If End Sub How do I use a column in the selected row to pass information into a variable which can then be used on the next form? Many thanks for any help you can give me.

        P Offline
        P Offline
        Pradeep C
        wrote on last edited by
        #3

        You could use this logic also to select a row, which has been clicked ( I am not sure which has better performance or whether this logic has a drawback) : If DataGrid1.CurrentRowIndex > -1 then DataGrid1.Select(DataGrid1.CurrentRowIndex) End if I think to get column value of selected row, you should get it from the underlying DataSet (or DataTable) using the CurrentRowIndex of the DataGrid For Eg, if your DataSet ds has a table which is shown in the DataGrid, and you want to get value of column with index 3 of currently selected row, col3Value = ds.Tables(0).Rows(DataGrid1.CurrentRowIndex).Item(3) But this logic will fail if you have used the RowFilter to filter rows from the DataSet's default view. This is because the row index of DataGrid and that in the DataSet wont be same. To overcome that you could use this logic to get the DataRow in the DataSet corresponding to the row selected in the DataGrid: Private Function GetDataRow() As DataRow Dim dr() As DataRow = ds.Tables(0).Select(ds.Tables(0).DefaultView.RowFilter) Return dr(DataGrid1.CurrentRowIndex) End Function From the returned DataRow, you can take the required value : col3Value = dr.Item(3) Hope it helped. ---------------------------------------------------------------------------- "I think there is a world market for maybe 5 computers" Thomas Watson, chairman of IBM, 1943 "There is no reason anyone would want a computer in their home" Ken Olson, chairman & founder of Digital equipment, 1977 "This 'telephone' has too many shortcomings to be seriously considered as a means of communication. The device is ingerently of no value to us." Western Union internal memo, 1876 "640 K ought to be enough for anybody." Bill Gates, 1981 "Computers in the future may

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups