By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,156 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,156 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Feb 16 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.