How to get GridView Cells Value

Posted by Self-Innovator under C# on 11/30/2012 | Points: 10 | Views : 2558 | Status : [Member] | Replies : 2
Hi hot to get gridview cell value in foreachloop please find me soln...
My code it returns only Header Text not cell value
for (int i = 0; i < dg.Rows.Count; i++)
{
strB.AppendLine("<tr>");
for(int j=0;j<dg.Columns.Count;j++)
{
strB.AppendLine("<td align='center' valign='middle'>" +
dg.Columns[j].ToString() + "</td>");
//Here i'm getting Header text value instead of cell value
}
strB.AppendLine("</tr>");

}


Join Hands Change lives
Thanks & Regards
Straight Edge Society



Responses

Posted by: Kundnani.Rt on: 1/19/2013 [Member] Starter | Points: 25

Up
0
Down
You can try this:

  protected void Page_Load(object sender, EventArgs e)

{
StringBuilder strB = new StringBuilder();

strB.Append("<table border=1>");

for (int i = 0; i < dg.Rows.Count; i++)
{
strB.AppendLine("<tr>");
if (i == 0)
{
for (int j = 0; j < dg.Columns.Count; j++)
{
strB.AppendLine("<td align='center' valign='middle'>" +
dg.Columns[j].ToString() + "</td>"); //Here i'm getting Header text value instead of cell value
}
}
else
{
for (int j = 0; j < dg.Columns.Count; j++)
{
strB.AppendLine("<td align='center' valign='middle'>" + dg.Rows[i].Cells[j].Text
+ "</td>");
}
}
strB.AppendLine("</tr>");

}


strB.Append("</table>");
this.Page.Form.InnerHtml=strB.ToString();
}


Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 1/21/2013 [Member] [MVP] Gold | Points: 25

Up
0
Down
Try this

foreach (GridViewRow row in GridView1.Rows)
{
strB.AppendLine("<tr>");
strB.AppendLine("<td align='center' valign='middle'>" +
row.Cells[0].ToString() + "</td>");
strB.AppendLine("<td align='center' valign='middle'>" +
row.Cells[1].ToString() + "</td>");
strB.AppendLine("</tr>");
}


Please Mark As Answer if this helps you

.

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response