Getting data from a selected row in a gridview

Discussion in 'ASP .Net' started by Neil, Nov 11, 2005.

  1. Neil

    Neil Guest

    I have a gridview which is being populated with no problems.

    I want to be able to reference the data from the cells in the row but
    having followed an example on MSDN cannot get any data to be displayed
    in a text box no matter which cell index I provide.

    I tried a row of code that successfully displays the rowindex of the
    selected row.

    The code with comments is below:

    protected void GridView1_SelectedIndexChanged(object sender,
    EventArgs e)
    {
    // Get the currently selected row using the SelectedRow
    property.
    GridViewRow row = GridView1.SelectedRow;

    //This row doesn't put anything in the text box (code copied
    from MSDN)
    TextBox2.Text = row.Cells[2].Text;

    //This row (when not commented out and line above commented
    out) puts the row index in the text box
    //TextBox2.Text = GridView1.SelectedRow.RowIndex.ToString();
    }

    Can anyone provide me with the code that will allow me to access the
    data in each cell. When working in the debugger I'm being told that
    the text value is an empty string which can't be correct coz i can see
    the value in the grid. I'm new to both ASP.Net & C# so may be doing
    something obviously wrong!

    Thanks
     
    Neil, Nov 11, 2005
    #1
    1. Advertising

  2. Neil

    Fred Exley Guest

    "Neil" <> wrote in message
    news:...
    >I have a gridview which is being populated with no problems.
    >
    > I want to be able to reference the data from the cells in the row but
    > having followed an example on MSDN cannot get any data to be displayed
    > in a text box no matter which cell index I provide.
    >
    > I tried a row of code that successfully displays the rowindex of the
    > selected row.
    >
    > The code with comments is below:
    >
    > protected void GridView1_SelectedIndexChanged(object sender,
    > EventArgs e)
    > {
    > // Get the currently selected row using the SelectedRow
    > property.
    > GridViewRow row = GridView1.SelectedRow;
    >
    > //This row doesn't put anything in the text box (code copied
    > from MSDN)
    > TextBox2.Text = row.Cells[2].Text;
    >
    > //This row (when not commented out and line above commented
    > out) puts the row index in the text box
    > //TextBox2.Text = GridView1.SelectedRow.RowIndex.ToString();
    > }
    >
    > Can anyone provide me with the code that will allow me to access the
    > data in each cell. When working in the debugger I'm being told that
    > the text value is an empty string which can't be correct coz i can see
    > the value in the grid. I'm new to both ASP.Net & C# so may be doing
    > something obviously wrong!
    >
    > Thanks
    >


    This works for me:

    //////////////////////////////////////////////////////////////
    //Use this method to copy the items in the GridViewRowCollection
    object
    //into the specified System.Array object, starting at the specified
    index.
    //The System.Array object can then be used to access the items in
    the collection.

    // Copy the items in the Rows collection into an array.
    GridViewRow[] rowArray = new GridViewRow[gv.Rows.Count];
    gv.Rows.CopyTo(rowArray, 0);

    // Iterate though the array and display the value in the first cell
    of the row.
    int j = -1;
    foreach (GridViewRow row in rowArray)
    {
    j++;
    if (j == idx)
    {
    Label1.Text = row.Cells[1].Text;
    }
    }
    //////////////////////////////////////////////////////////////



    Apparantly the key is to copy the rows into an array to be able to iterate
    through it.
    I got the basic idea from :
    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrowcollection.aspx
    -Fred
     
    Fred Exley, Nov 11, 2005
    #2
    1. Advertising

  3. Neil

    Neil Guest

    Hi Fred,

    Thanks for the reply.
    I used your example but still can't get anything to display.
    I'm stumped

    The code I used was:

    // Copy the items in the Rows collection into an array.
    GridViewRow[] rowArray = new GridViewRow[GridView1.Rows.Count];
    GridView1.Rows.CopyTo(rowArray, 0);

    // Iterate though the array and display the value in the sixth
    cell of the row.
    int j = -1;
    foreach (GridViewRow row in rowArray)
    {
    j++;
    if (j ==GridView1.SelectedRow.RowIndex)
    {
    TextBox2.Text = row.Cells[5].Text;
    }
    }
     
    Neil, Nov 11, 2005
    #3
  4. Neil

    Fred Exley Guest

    "Neil" <> wrote in message
    news:...
    > Hi Fred,
    >
    > Thanks for the reply.
    > I used your example but still can't get anything to display.
    > I'm stumped
    >
    > The code I used was:
    >
    > // Copy the items in the Rows collection into an array.
    > GridViewRow[] rowArray = new GridViewRow[GridView1.Rows.Count];
    > GridView1.Rows.CopyTo(rowArray, 0);
    >
    > // Iterate though the array and display the value in the sixth
    > cell of the row.
    > int j = -1;
    > foreach (GridViewRow row in rowArray)
    > {
    > j++;
    > if (j ==GridView1.SelectedRow.RowIndex)
    > {
    > TextBox2.Text = row.Cells[5].Text;
    > }
    > }
    >


    This works on my page, displaying a specific cell of the selected row:
    Label4.Text = Convert.ToString(GridView3.SelectedRow.Cells[1].Text); // Food
    item desc

    If that doesn't help, I can send you my complete app and you can work back
    from there.

    -Fred
     
    Fred Exley, Nov 11, 2005
    #4
  5. Neil

    Neil Guest

    Still can't get it to work.

    I don't understand this at all. The code looks fine, the grid has data
    in it and if I use this line:
    TextBox2.Text = GridView1.SelectedRow.RowIndex.ToString();

    I get a value displayed in the text box which indicates to me that the
    code is seeing a row (at least) in the grid.

    I'd be grateful for a copy of your app as I'm tearing my hair out here
    trying to solve what should be a trivial matter.
     
    Neil, Nov 11, 2005
    #5
  6. Neil

    SueL

    Joined:
    Sep 22, 2006
    Messages:
    1
    GridView: retireving selected row data

    Fred,

    I am having the same problem as Neil.

    Could I also have a copy of your app?

    - many thanks in advance

    Sue
     
    SueL, Sep 22, 2006
    #6
  7. Neil

    chookieeee

    Joined:
    Apr 17, 2007
    Messages:
    1
    after taking a good 2.4 hours of my day,
    i worked it out... altho i dont know why this works...

    string s = GridView1.SelectedRow.Cells[2].Text;

    the '2' seems to give me my first column... i have no idea why.

    ow well. it works. have fun.
     
    chookieeee, Apr 17, 2007
    #7
  8. Neil

    Moim

    Joined:
    Jun 6, 2007
    Messages:
    2
    Location:
    Dhaka
    I guess, the best thing to do this is as follows

    For example if you have a row where the first column is a link button into the item template then your approach should be

    Control ctl = row.Controls[0];// get the first control reference
    string value = ((System.Web.UI.WebControls.LinkButton)(ctl.Controls[1])).Text; // if the control you defined in Item template mark up is a link button;
    // otherwise you need to do the appropriate casting :)
     
    Moim, Jun 7, 2007
    #8
  9. Neil

    m6azeez

    Joined:
    Aug 8, 2007
    Messages:
    1

    you are the best :flute: :flute:

    thanks a lot dude I like your simple and correct way of solving this issue
    it works perfectly

    thanks again
     
    m6azeez, Aug 8, 2007
    #9
  10. Neil

    andyv

    Joined:
    Nov 25, 2009
    Messages:
    1
    a way round this problem is to get the keydata from the gridview:

    inside the gridview tag include the field you want to access in this field:

    Code:
    <asp:GridView ID="grdCalls" runat="server" DataKeyNames="FolderID, FolderName">
    then in the event method:

    Code:
    grdCalls_SelectedIndexChanged(object sender, EventArgs e)
    {
    string FolderID = grdCalls.SelectedDataKey[0].ToString()
    
    }
    should do the trick nicely
     
    andyv, Nov 25, 2009
    #10
  11. Neil

    markieshorty

    Joined:
    Jan 19, 2011
    Messages:
    1
    Solution to problem if using item templates in gridview

    SelectedRow.Cells[] function does not work if you have template fields in your gridview.

    Solution to problem if using item templates in as gridview columns:

    Label lbl = (Label)GridView1.SelectedRow.FindControl("Label1");
    string str = lbl.Text;


    Also, it was SelectedRow.Cells[2] because cells[0] was probably an edit cell and cells[1] was probably a delete cell or vice versa.
     
    markieshorty, Jan 19, 2011
    #11
  12. Neil

    bartekm

    Joined:
    Aug 17, 2008
    Messages:
    7
    bartekm, Jul 5, 2011
    #12
  13. Neil

    magesh

    Joined:
    Feb 8, 2012
    Messages:
    1
    protected void gvInventory_SelectedIndexChanged(object sender, EventArgs e)
    {
    //int orderID = (int)this.gvInventory.SelectedRow.Cells.Count;

    GridView dummyinv = new GridView();
    //int rowval = gvInventory.Rows.Count;
    GridViewRow row = gvInventory.SelectedRow;
    //string name = row.Cells[0].Text;
    //select the row count value
    string name = gvInventory.SelectedRow.RowIndex.ToString();
    query = "select TireCompany,TireTreadType,TireSize,TireTreadDepth,TireTreadDepthC,Price,TireDiameter,TireCircumference,Ref,GoneDate,Customer from TblTireInventoryDetails where TireCompany=@cname";
    OleDbCommand cmd = new OleDbCommand();
    cmd.Parameters.AddWithValue("@cname", name);
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = query;
    cmd.Connection = conn;
    try
    {
    conn.Open();
    OleDbDataReader dr = cmd.ExecuteReader();

    if (dr.HasRows)
    {
    while (dr.Read())
    {
    ddlCompany.SelectedItem.Text = dr[0].ToString();
    ddlTreaddesign.SelectedItem.Text = dr[1].ToString();
    ddlTiresize.SelectedItem.Text = dr[2].ToString();
    txtOTDepth.Text = dr[3].ToString();
    txtCTDepth.Text = dr[4].ToString();
    txtPrice.Text = dr[5].ToString();
    txtDiameter.Text = dr[6].ToString();
    txtCircumference.Text = dr[7].ToString();
    txtRef.Text = dr[8].ToString();
    ddlDate.SelectedItem.Text = dr[9].ToString();
    ddlCompanyout.SelectedItem.Text = dr[10].ToString();
    //txtFax.Text = dr[11].ToString();
    //txtPassword.Text = dr[12].ToString();
    //break;

    }
    dr.Close();
    }



    }
    catch (Exception ex)
    {
    throw ex;
    }
    finally
    {
    conn.Close();
    }

    }


    i think its very usefull to you..........
    P.Magesh
     
    magesh, Feb 8, 2012
    #13
  14. Neil

    Eng.Eg

    Joined:
    Apr 24, 2012
    Messages:
    1
    Thank You

    chookieeee thank you so much i spent like 4 or 5 hours trying to solve this problem really
    THANK YOU:congrats:
     
    Eng.Eg, Apr 24, 2012
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil
    Replies:
    0
    Views:
    760
  2. Kirk
    Replies:
    2
    Views:
    1,773
  3. Paul
    Replies:
    3
    Views:
    780
  4. Want to add row under selected row of gridview

    , Feb 16, 2007, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    319
  5. Selected Row Only GridView Row Visible?

    , Oct 17, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    307
Loading...

Share This Page