locked
Get value from gridview column

    Question

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

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

    Wednesday, May 24, 2006 12:56 PM

Answers

  • 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...
    Wednesday, May 24, 2006 1:14 PM

All replies

  • 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...
    Wednesday, May 24, 2006 1:14 PM
  • 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
    Wednesday, May 24, 2006 1:16 PM
  • 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

     

     

    Thursday, May 25, 2006 6:43 AM
  • Thanks, it's very interesting
    Thursday, May 25, 2006 6:46 AM
  • 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?

    • Proposed as answer by Winston Muller Wednesday, April 01, 2009 8:47 AM
    Thursday, June 28, 2007 12:14 PM
  •  

    Try this..equivalent to VB.net

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

     

     

    Thursday, November 15, 2007 3:11 PM
  • Thanks a lot
    Thursday, July 09, 2009 11:48 AM
  • 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...

    Excellent
    Saturday, October 23, 2010 2:37 PM
  • For Each row As DataGridViewRow In datadv.SelectedRows
        txtbox1.Text = row.Cells("Slno").Value.ToString()
        txtbox2.Text = row.Cells("Name").Value.ToString()
    Next

     

    try this...

    Wednesday, October 12, 2011 9:45 AM
  • Hi, I'm coding C# script in Test Complete tool. I get the value from grid by function:

    function FindRowByCellValue (Grid, ChildView, ColName, SoughtValue){

      var Val, i;

      if (ChildView == null)

        ChildView = Grid;

        Val = VarToStr (SoughtValue);

      // Iterate through grid rows 

      for (i = 0; i < ChildView["wRowCount"]; i++)

        // Compare the cell value with the specified value

        if (VarToStr(ChildView["wValue"](i,ColName)) == Val)

          ShowMessage("Value at row " + i);   

      ShowMessage("Value not found in any row!");   

    }

    Have anyway to get all items in grid? 

    Thanks.
    • Edited by MiracleRose Sunday, December 23, 2012 4:23 AM
    Thursday, October 13, 2011 8:12 AM