如何在datatable中附加数据

时间:2013-02-07 08:47:38

标签: c# asp.net gridview datatable

我有两个文本框和一个按钮。在按钮单击事件中,我将文本框中捕获的值添加为我的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>

1 个答案:

答案 0 :(得分:2)

这是一个适合你的代码

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