【问题标题】:how to append data in datatable如何在数据表中追加数据
【发布时间】:2013-01-22 17:00:57
【问题描述】:

我有两个文本框和一个按钮。在按钮单击事件中,我将在文本框中捕获的值添加为我的 grdiview 中的新行。现在我想要的是当我的页面再次加载时,我的 gridview 将显示我添加到其中的现有数据并在不使用数据库的情况下追加新记录。

这是我的代码:

private void BindGrid(int rowcount)
{
    DataTable dt = new DataTable();
    DataRow dr;

    dt.Columns.Add("First Name", typeof(String));
    dt.Columns.Add("Last Name", typeof(String));

    if (ViewState["CurrentData"] != null)
    {
        for (int i = 0; i < rowcount + 1; i++)
        {
            dt = (DataTable)ViewState["CurrentData"];
            if (dt.Rows.Count > 0)
            {
                dr = dt.NewRow();
                dr[0] = dt.Rows[0][0].ToString();

            }
        }
        dr = dt.NewRow();
        dr[0] = TextBox1.Text;
        dr[1] = TextBox2.Text;
        dt.Rows.Add(dr);
    }
    else
    {
        dr = dt.NewRow();
        dr[0] = TextBox1.Text;
        dr[1] = TextBox2.Text;

        dt.Rows.Add(dr);
    }

    // If ViewState has a data then use the value as the DataSource
    if (ViewState["CurrentData"] != null)
    {
        GridView1.DataSource = (DataTable)ViewState["CurrentData"];
        GridView1.DataBind();
    }
    else
    {
        // Bind GridView with the initial data assocaited in the DataTable
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    // Store the DataTable in ViewState to retain the values
    ViewState["CurrentData"] = dt;
}

按钮点击事件:

protected void Button1_Click(object sender, EventArgs e)
{
    // Check if the ViewState has a data assoiciated within it. If
    if (ViewState["CurrentData"] != null)
    {
        DataTable dt = (DataTable)ViewState["CurrentData"];
        int count = dt.Rows.Count;
        BindGrid(count);
    }
    else
    {
        BindGrid(1);
    }
    TextBox1.Text = string.Empty;
    TextBox2.Text = string.Empty;
    TextBox1.Focus();
}

这是我的 ASPX 页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs"   Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
     </head>
    <body>
        <form id="form1" runat="server">
       <div>

        <asp:TextBox ID="TextBox1" runat="server"/>&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox2" runat="server"/>&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:GridView          ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None" > <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#2461BF" />
         <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
         <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
         <SortedDescendingCellStyle BackColor="#E9EBEF" />
         <SortedDescendingHeaderStyle BackColor="#4870BE" />
       </asp:GridView>

        &nbsp;</div>
        </form>
    </body>
  </html>

【问题讨论】:

  • 创建一个新的 dt 行并将值放入每一列并将其添加到 dt 并添加回 viewstate
  • @krshekhar 你能用代码解释一下吗
  • @amitesh 你的意思是你希望能够导航到其他页面,然后返回到 this 特定页面并查看填充了之前捕获的值的网格视图会议期间?
  • 看到我现在使用添加按钮在网格视图中添加一些数据,当我再次打开我的页面并单击显示按钮时,我可以查看那些我想要的输入数据

标签: c# asp.net gridview datatable


【解决方案1】:

这是一个适合你的代码

private void BindGrid()
{
    GridView1.DataSource = GetDataTable();
    GridView1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
    DataRow dr;
    DataTable dt = GetDataTable();
    dr = dt.NewRow();
    dr[0] = TextBox1.Text;
    dr[1] = TextBox2.Text;
    dt.Rows.Add(dr);
    ViewState["CurrentData"] = dt;
    TextBox1.Text = string.Empty;
    TextBox2.Text = string.Empty;
    TextBox1.Focus();
    //**updated**
    BindGrid();
}

protected DataTable GetDataTable()
{
    DataTable dt;
    if (ViewState["CurrentData"] != null)
    {
        dt = (DataTable)ViewState["CurrentData"];
    }
    else
    {
        dt = new DataTable();
        dt.Columns.Add("First Name", typeof(String));
        dt.Columns.Add("Last Name", typeof(String));
        //**Update**/
        ViewState["CurrentData"] = dt;
    }
    return dt;
}

并在页面加载时调用函数BindGrid

编辑-1

如果您在访问另一个页面后想要它,那么您应该使用session 而不是viewstate
相应地更改功能

protected DataTable GetDataTable()
{
    DataTable dt;
    if (Session["CurrentData"] != null)
    {
        dt = (DataTable)Session["CurrentData"];
    }
    else
    {
        dt = new DataTable();
        dt.Columns.Add("First Name", typeof(String));
        dt.Columns.Add("Last Name", typeof(String));
        Session["CurrentData"] = dt;
    }
    return dt;
}

【讨论】:

  • 当我点击添加按钮时它不起作用我将无法查看我的网格视图
  • @amitesh 在按钮单击功能结束时调用BindGrid() 我错过了。我正在相应地更新我的帖子
  • 你能告诉我我们如何显示一个网格的数据来自数据表到另一个
  • @amitesh 你可以使用相同的data-source grid
  • 谢谢@krshekher,我已经这样做了,但是感谢您的回答,现在我还有一个任务要从我的第一个gridview中选择和删除行,我正在这样做,如果您能帮助我,我会感谢您
猜你喜欢
  • 1970-01-01
  • 2013-05-13
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多