Help: How to read data from each row of a GridView?

Discussion in 'ASP .Net' started by Hongbo, Feb 16, 2006.

  1. Hongbo

    Hongbo Guest

    Hi,

    I have a GridView like this one:
    ==
    <asp:gridview runat="server" id="gvCategory" allowpaging="true" allowsorting="true"
    alternatingrowstyle-backcolor="#B9DFC7" autogeneratecolumns="false"
    pagesize="20" width="550px" datasourceid="prodList">
    <headerstyle backcolor="#B9DFC7" />
    <columns>
    <asp:templatefield>
    <itemtemplate>
    <asp:checkbox runat="server" id="chkAddToCart" />
    </itemtemplate>
    </asp:templatefield>
    <asp:boundfield headertext="productID" datafield="productID" visible="false" />
    <asp:templatefield>
    <itemtemplate>
    <a href='<%# "/product/productDetail.aspx?productID="
    +DataBinder.Eval(Container.DataItem, "productId") %>'><img alt="" src='<%# "/images/prod/"
    +DataBinder.Eval(Container.DataItem, "thumbImg") %>' style="border:none" /></a>
    </itemtemplate>
    </asp:templatefield>
    </columns>
    </asp:gridview>
    ==

    In .Net 1.x, the DataGrid works with the following code:
    ==
    foreach (DataGridItem dgi in DataGrid1.Items)
    {
    chb = (CheckBox)dgi.FindControl("chkAddToCart");
    if (chb.Checked)
    {
    string pid=dgi.Cells[1].Text;
    }
    }
    ==

    So I tried:
    ==
    foreach (GridViewRow gvr in gvCategory.Rows)
    {
    if(gvr.HasControls()==true)
    {
    chb = (CheckBox)gvr.FindControl("chkAddToCart");

    if (chb.Checked)
    {
    string pid = gvr.Cells[1].Text;
    }
    }
    }
    ==
    But gvr.Cells[1].Text returns nothing. What's wrong here?

    Would you please tell me how to read data from the 2nd field--the Cells[1]
    (i.e. <asp:boundfield headertext="productID" datafield="productID" visible="false" />)
    in each row of the above GridView?

    Thank you

    hb
     
    Hongbo, Feb 16, 2006
    #1
    1. Advertising

  2. Hongbo

    georgeRizk

    Joined:
    May 29, 2012
    Messages:
    1
    //The Folowing Code works fine for me
    protected void btnAdd_Click(object sender, EventArgs e)
    {
    foreach (GridViewRow gvr in grdProducts.Rows)
    {

    CheckBox chk = (CheckBox)gvr.Cells[2].FindControl("chkHDOF");
    if (chk != null && chk.Checked)
    {
    this.lblBatch.Text += gvr.Cells[0].Text;
    }
    }
    }
     
    georgeRizk, May 29, 2012
    #2
    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. John Blair
    Replies:
    1
    Views:
    419
    Eliyahu Goldin
    Aug 3, 2005
  2. H5N1
    Replies:
    0
    Views:
    2,739
  3. Replies:
    5
    Views:
    678
  4. Ken Fine
    Replies:
    4
    Views:
    395
    Steven Cheng [MSFT]
    Jul 14, 2008
  5. D
    Replies:
    0
    Views:
    229
Loading...

Share This Page