我正在使用asp.net和c#
我需要从文本框值填充gridview,
我的代码是,
<div>
<table class="style1">
<tr>
<td>
Name
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
Address
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
Number
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Add" />
</td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</td>
</tr>
</table>
</div>
然后我的.aspx代码是,
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Name");
dt.Columns.Add("Address");
dt.Columns.Add("Number");
//First fill all the date present in the grid
for (int intCnt = 0; intCnt < GridView1.Rows.Count; intCnt ++)
{
if (GridView1.Rows[intCnt].RowType == DataControlRowType.DataRow)
{
dr = dt.NewRow();
dr["Name"] = GridView1.Rows[intCnt].Cells[0];
dr["Address"] = GridView1.Rows[intCnt].Cells[1];
dr["Number"] = GridView1.Rows[intCnt].Cells[2];
dt.Rows.Add(dr);
}
}
dr = dt.NewRow();
dr["Name"] = TextBox1.Text;
dr["Address"] = TextBox2.Text;
dr["Number"] = TextBox3.Text;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
但结果是,添加带有值的单行... 下一行值未正确添加... 问题是, 文本框值的第二个条目存储为&#34; System.Web.UI.WebControls.DataControlFieldCell&#34;显示在第二行的每个单元格中......
答案 0 :(得分:2)
它应该像
dr["Address"] = GridView1.Rows[intCnt].Cells[1].Text;
这是你没有正确获得价值的原因。
for (int intCnt = 0; intCnt < GridView1.Rows.Count; intCnt ++)
{
if (GridView1.Rows[intCnt].RowType == DataControlRowType.DataRow)
{
dr = dt.NewRow();
dr["Name"] = GridView1.Rows[intCnt].Cells[0].Text;
dr["Address"] = GridView1.Rows[intCnt].Cells[1].Text;
dr["Number"] = GridView1.Rows[intCnt].Cells[2].Text;
dt.Rows.Add(dr);
}
}
答案 1 :(得分:0)
使用for循环提取网格视图行,并使用每个单元格的Text
属性再次分配给DataTable
。
这是您的点击命令:
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Name");
dt.Columns.Add("Address");
dt.Columns.Add("Number");
foreach (GridViewRow gr in GridView1.Rows)
{
//using gridviewrow
if (gr.RowType == DataControlRowType.DataRow)
{
dr = dt.NewRow();
dr["Name"] = gr.Cells[0].Text;
dr["Address"] = gr.Cells[1].Text;
dr["Number"] = gr.Cells[2].Text;
dt.Rows.Add(dr);
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
答案 2 :(得分:0)
您正在分配单元格的对象而不是值。
获取单元格的Text属性。
protected void Button1_Click1(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Name");
dt.Columns.Add("Address");
dt.Columns.Add("Number");
//First fill all the date present in the grid
for (int intCnt = 0; intCnt < GridView1.Rows.Count; intCnt++)
{
if (GridView1.Rows[intCnt].RowType == DataControlRowType.DataRow)
{
dr = dt.NewRow();
dr["Name"] = GridView1.Rows[intCnt].Cells[0].Text;
dr["Address"] = GridView1.Rows[intCnt].Cells[1].Text;
dr["Number"] = GridView1.Rows[intCnt].Cells[2].Text;
dt.Rows.Add(dr);
}
}
dr = dt.NewRow();
dr["Name"] = TextBox1.Text;
dr["Address"] = TextBox2.Text;
dr["Number"] = TextBox3.Text;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}