C# 如何在asp.NETC中从gridview中提取行#
我已经创建了保存在会话中并分配给gridview的datatable。我必须提取这些记录,为此我编写了如下代码,但其给定值为null:C# 如何在asp.NETC中从gridview中提取行#,c#,asp.net,C#,Asp.net,我已经创建了保存在会话中并分配给gridview的datatable。我必须提取这些记录,为此我编写了如下代码,但其给定值为null: //To find gridview in Formview1 GridView GridView1 = (GridView)Formview1.FindControl("GridView1"); GridView1.DataSource = (DataTable)Session["tabdetails"]; GridView1.Data
//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;
}
//在Formview1中查找gridview
GridView GridView1=(GridView)Formview1.FindControl(“GridView1”);
GridView1.DataSource=(DataTable)会话[“tabdetails”];
GridView1.DataBind();
字符串str、str1、str2、str3、str4、str5、str6;
对于(int i=0;i
请建议我做正确的改变?
非常感谢。
Asp.net c#使用数据键存储需要检索的值
<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"];
}
for(int i=0;i
使用数据键存储需要检索的值
<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"];
}
for(int i=0;i
您可以使用for each循环提取网格视图行
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");
}
}
可以使用for each循环提取栅格视图行
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");
}
}
如果gridview单元格中有控件,则文本值将为空。如果你有一个简单的文本框,你应该修改你的代码来使用“控件”集合
str = ((TextBox)GridView1.Rows(i).Cells(0).Controls(1)).Text;
使用索引[1]而不是[0],因为索引[0]通常是文本控件。如果gridview单元格中有控件,则文本值将为空。如果你有一个简单的文本框,你应该修改你的代码来使用“控件”集合
str = ((TextBox)GridView1.Rows(i).Cells(0).Controls(1)).Text;
使用索引[1]而不是[0],因为索引[0]通常是一个文本控件。如果您不打算编辑UI中的内容,请直接从
会话[“tabdetails”]获取它代码>。这会容易得多。如果要更改UI中的内容,请在单元格内找到对应的文本框控件,并从中获取值。如果不打算编辑UI中的内容,请直接从会话[“tabdetails”]中获取它代码>。这会容易得多。如果要更改UI中的内容,请在单元格内找到对应的文本框控件,并从中获取值。