none
How to get values from selected row in datatable on CellClick Event RRS feed

  • Question

  • Hi just want to ask on how can I get the values from a row in datatable

    ex.:

    datatable

    col1 | col2 | col3 | col4

    c1r1 | c2r1 | c3r1 | c4r1

    c1r2 | c2r2 | c3r2 | c4r2

    in datagridview col2 is hidden for display purposes. col2 holds the ID values which is not included in the display

    so if I click lets say for example c4r1 from datagridview I can get the value of col2 which is c2r1 and put in a label.

    I've search on how to do it but I can't get the result I want. Hope you can help me with this.


    GEEDER

    Sunday, November 4, 2018 10:27 PM

Answers

  • Here is an example where there are three DataColumn, two visible, one hidden.

    Load mocked up DataTable to DataSource of a DataGridView.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        Dim dt As New DataTable
    
        dt.Columns.Add(New DataColumn With {.ColumnName = "Id", .DataType = GetType(Integer),
                            .ColumnMapping = MappingType.Hidden})
        dt.Columns.Add(New DataColumn With {.ColumnName = "Name", .DataType = GetType(String)})
        dt.Columns.Add(New DataColumn With {.ColumnName = "Grade", .DataType = GetType(Integer)})
    
        dt.Rows.Add(New Object() {1, "Mary", 20})
        dt.Rows.Add(New Object() {2, "john", 30})
        dt.Rows.Add(New Object() {3, "Frank", 40})
        dt.Rows.Add(New Object() {4, "Adam", 30})
    
        DataGridView1.DataSource = dt
    
    End Sub

    Get the current DataGridView row value for the hidden field Id. Note the variable currentRow is our current DataRow obtained via DataGridView1.CurrentRow.Index

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim currentRow As DataRow = CType(DataGridView1.DataSource, DataTable).
                Rows(DataGridView1.CurrentRow.Index)
    
        MessageBox.Show(currentRow.Field(Of Integer)("Id").ToString())
    
    End Sub
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by GEEDER Sunday, November 4, 2018 10:58 PM
    Sunday, November 4, 2018 10:52 PM
    Moderator

All replies

  • Here is an example where there are three DataColumn, two visible, one hidden.

    Load mocked up DataTable to DataSource of a DataGridView.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        Dim dt As New DataTable
    
        dt.Columns.Add(New DataColumn With {.ColumnName = "Id", .DataType = GetType(Integer),
                            .ColumnMapping = MappingType.Hidden})
        dt.Columns.Add(New DataColumn With {.ColumnName = "Name", .DataType = GetType(String)})
        dt.Columns.Add(New DataColumn With {.ColumnName = "Grade", .DataType = GetType(Integer)})
    
        dt.Rows.Add(New Object() {1, "Mary", 20})
        dt.Rows.Add(New Object() {2, "john", 30})
        dt.Rows.Add(New Object() {3, "Frank", 40})
        dt.Rows.Add(New Object() {4, "Adam", 30})
    
        DataGridView1.DataSource = dt
    
    End Sub

    Get the current DataGridView row value for the hidden field Id. Note the variable currentRow is our current DataRow obtained via DataGridView1.CurrentRow.Index

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim currentRow As DataRow = CType(DataGridView1.DataSource, DataTable).
                Rows(DataGridView1.CurrentRow.Index)
    
        MessageBox.Show(currentRow.Field(Of Integer)("Id").ToString())
    
    End Sub
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by GEEDER Sunday, November 4, 2018 10:58 PM
    Sunday, November 4, 2018 10:52 PM
    Moderator
  • Hi

    Here is my contribution (late to the table:)

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim dt As New DataTable
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With dt
          .Columns.Add("Col1")
          .Columns.Add("Col2")
          .Columns.Add("Col3")
          .Columns.Add("Col4")
          .Rows.Add("C1R1", "C2R1", "C3R1", "C4R1")
          .Rows.Add("C1R2", "C2R2", "C3R2", "C4R2")
          .Rows.Add("C1R3", "C2R3", "C3R3", "C4R3")
          .Rows.Add("C1R4", "C2R4", "C3R4", "C4R4")
        End With
        DataGridView1.DataSource = dt
        DataGridView1.Columns("Col2").Visible = False
      End Sub
      Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Try
          Label1.Text = dt.Rows(e.RowIndex).Item(1).ToString
        Catch ex As IndexOutOfRangeException
          Exit Sub
        End Try
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, November 4, 2018 10:53 PM
    Sunday, November 4, 2018 10:53 PM