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

H

Hongbo

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
 
Joined
May 29, 2012
Messages
1
Reaction score
0
//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;
}
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top