[英]How to extract rows from gridview in asp.net c#
我创建了保存在 session 中的数据表并分配给 gridview。我必须提取这些记录,因为我编写了如下代码,但它的给定值为 null:
//To find gridview in Formview1
GridView GridView1 = (GridView)Formview1.FindControl("GridView1");
GridView1.DataSource = (DataTable)Session["tabdetails"];
GridView1.DataBind();
string str, str1, str2, str3, str4, str5, str6;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
str = GridView1.Rows[i].Cells[0].Text;
str1 = GridView1.Rows[i].Cells[1].Text;
str3 = GridView1.Rows[i].Cells[2].Text;
str4 = GridView1.Rows[i].Cells[3].Text;
str5 = GridView1.Rows[i].Cells[4].Text;
}
请建议我做正确的改变? 谢谢你。 Asp.net c#
您可以使用每个循环提取网格视图行
foreach (GridViewRow GR in GridView1.Rows)
{
if (GR.RowType == DataControlRowType.DataRow)
{
string myField = GR.Cells[1].Text;
// You can also find grid view inside controls here
TextBox tb = (TextBox)tt.FindControl("TextBox1");
}
}
使用数据键存储您需要检索的值。
<asp:GridView ID="GridView1" runat="server" DataKeyNames="SomeColumnName, AnotherColumnName, SomeOtherColumnName" ... >
在你后面的代码中:
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
string str = GridView1.DataKeys[row.RowIndex]["SomeColumnName"];
}
如果您的 gridview 单元格中有控件,则 Text 值将为空。 如果您有一个简单的文本框,您应该修改您的代码以使用“控件”集合。
str = ((TextBox)GridView1.Rows(i).Cells(0).Controls(1)).Text;
使用索引 [1] 而不是 [0] 因为通常索引 [0] 将是一个文字控件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.