如何将文本框值分配给gridview

本文关键字:分配 gridview 文本 | 更新日期: 2023-09-27 18:02:53

我正在使用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();
    }

,但结果是,添加单行值…下一行值没有正确添加…问题是,文本框值的第二个条目存储为"System.Web.UI.WebControls"。

如何将文本框值分配给gridview

应该是

  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);
            }
        }

为每个循环提取网格视图行,并使用每个单元格的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();
}

您正在分配单元格对象而不是值。

获取单元格的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();
}