将数据添加到gridview

时间:2016-05-24 22:22:56

标签: c# asp.net gridview

这是我的代码iam尝试在gridview中显示数据,该数据在点击提交按钮后输入到输入文本框字段中。

没有错误,但数据没有添加到表中,只显示空表

public partial class Default : System.Web.UI.Page
{
    DataRow dr;
    DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
           DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        dt.Columns.Add(new DataColumn("ID", typeof(int)));
        dt.Columns.Add(new DataColumn("Name", typeof(string)));
        dt.Columns.Add(new DataColumn("Salary", typeof(int)));
        dt.Columns.Add(new DataColumn("Department", typeof(string)));

        dr = dt.NewRow();
        dr["ID"] = txtID.Text;
        dr["Name"] = txtName.Text;
        dr["Salary"] = txtSalary.Text;
        dr["Department"] = txtDepartment.Text;
        dt.Rows.Add(dr);

        GridView1.DataSource = dt;
        GridView1.DataBind();

1 个答案:

答案 0 :(得分:1)

记住web是无状态的,这意味着每次你想要向gridview添加一行时,你需要将当前数据存储在某个地方。下面的例子使用ViewState。当然还有很多方法来存储状态ViewState中的ViewState,localStorage在浏览器和数据库存储中,仅举几例。

代码背后:

  protected void Page_Load(object sender, EventArgs e)
    {
    }

    private void DataBind()
    {
        DataTable table = ViewState["Data"] as DataTable;
        GridView1.DataSource = table;
        GridView1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        DataRow dr = null;
        DataTable dt = ViewState["Data"] as DataTable;

        if (dt == null)
        {
            dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID", typeof(int)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Salary", typeof(int)));
            dt.Columns.Add(new DataColumn("Department", typeof(string)));
        }

        dr = dt.NewRow();
        dr["ID"] = txtID.Text;
        dr["Name"] = txtName.Text;
        dr["Salary"] = txtSalary.Text;
        dr["Department"] = txtDepartment.Text;

        dt.Rows.Add(dr);

        ViewState["Data"] = dt;

        DataBind();
    }

<强> .ASPX:

<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td>ID</td>
                <td><asp:TextBox ID="txtID" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Name</td>
                <td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Salary</td>
                <td><asp:TextBox ID="txtSalary" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Department</td>
                <td><asp:TextBox ID="txtDepartment" runat="server"></asp:TextBox></td>
                <td><asp:Button ID="Button1" runat="server" Text="Add to grid" OnClick="Button1_Click" /></td>
            </tr>
        </table>
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </form>
</body>

<强>输出: Adding data to gridview