how to retrieve cell value from grid view using button column in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rameshgohil
    New Member
    • Oct 2007
    • 1

    how to retrieve cell value from grid view using button column in c#

    I am using grid view and a button column in it using <itemTemplate >
    but I am not able to rerive cell value of a selected row from grid view.
    I have tried the following to methods in Row_command event of gridview but it gives Null reference exception error while clicking the button in button column of grid view :
    [code=cpp]
    int index = gdvOdetails.Sel ectedIndex;
    object oid = (object)gdvOdet ails.SelectedDa taKey.Values["o_id"];
    txtMsg.Text = oid.ToString();

    for (int i = 0; i < gdvOdetails .Rows .Count ; i++)
    {
    if (gdvOdetails.Se lectedIndex == i)
    {
    txtMsg .Text =gdvOdetails.Ro ws[i].Cells[1].Text;
    }
    }
    [/code]
    can any one help to retrieve cell value of grid in any variable using button column
    Last edited by Frinavale; Feb 5 '08, 03:17 PM. Reason: Added [code] tags
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by rameshgohil
    I am using grid view and a button column in it using <itemTemplate >
    but I am not able to rerive cell value of a selected row from grid view.
    I have tried the following to methods in Row_command event of gridview but it gives Null reference exception error while clicking the button in button column of grid view :
    [code=cpp]
    int index = gdvOdetails.Sel ectedIndex;
    object oid = (object)gdvOdet ails.SelectedDa taKey.Values["o_id"];
    txtMsg.Text = oid.ToString();

    for (int i = 0; i < gdvOdetails .Rows .Count ; i++)
    {
    if (gdvOdetails.Se lectedIndex == i)
    {
    txtMsg .Text =gdvOdetails.Ro ws[i].Cells[1].Text;
    }
    }
    [/code]
    can any one help to retrieve cell value of grid in any variable using button column
    Unless your button is a Select button, your gdvOdetails.Sel ectedIndex will be nothing (because no row is selected). Try using a "select" button instead of a command button in your GridView.

    -Frinny

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      If the button they clicked is IN the row that you want, you can get the row/column indexes from the event.

      Comment

      Working...