Get value from gridview column

How to get value (text) from gridview control in VB.

textbox.text = (value in column2 of selected row in gridview)



Answer this question

Get value from gridview column

  • gulatis

    Try this..equivalent to VB.net

    string key = GridView1.DataKeys[e.RowIndex].Value.ToString();



  • Tomas W

    TextBox.Text = DataGridView1.Item(1, DataGridView1.SelectedRows(0).Index).Value
    BTW, I suggest to don't use "TextBox" as a name for a textbox, this make the programmer confuse...


  • dano992

    Thanks, this is very good idea, i using and folowed code

    Dim row As GridViewRow = GridView1.SelectedRow

    Dim key As String = GridView1.SelectedDataKey.Value.ToString

    txtDrzava.Text = row.Cells(1).Text

    txtDrNaziv.Text = row.Cells(2).Text


  • wendallsan

    Thanks, it's very interesting
  • Sandra Geisler

    Hi there,

    Perhaps something like:

    ProductsDataGridView.SelectedRows(0).Cells(1).Value.ToString()

    In the above ProductsDataGridView is the name of my DataGridView control. What the above line will do is get the first selected row (or the only selected row if the user only selects one at a time) and returns a string representation of the value in the second column of the row.

    In your code, you'd prolly need to add some checks like making sure the user has actually selected a row.

    Hope that helps a bit, but sorry if it doesn't

    PS. Just saw the post by Moayad after I put my post up. I think it's better than mine


  • Jason Smith - MSFT

    I try it but i get one error..

    Dim row As GridViewRow = GridView1.SelectedRow

    Dim key As String = GridView1.SelectedDataKey.Value.ToString ( Object reference not set to an instance of an object. )

    txtDrzava.Text = row.Cells(1).Text

    txtDrNaziv.Text = row.Cells(2).Text

    Somebody know why


  • Get value from gridview column