0

Background

So I just started working in ASP.Net a few days ago. On my webpage I am creating a contact table for a user to populate and then submit to a database. Now what I have the user doing is entering information into various text boxes and then clicking an "Add Contact" button which then adds the content to a DataTable which is stored in a Session object. The DataSource of my GridView is the DataTable.

Current State

Updated Click Event Code

Whenever I click the "Add Contact" button a new row is added to my GridView, but nothing shows up in the cells of the GridView.

Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            dt.Columns.Add("First", typeof(String));
            dt.Columns.Add("Last", typeof(String));
            dt.Columns.Add("Email", typeof(String));
            dt.Columns.Add("Phone", typeof(String));
            Session["TempTable"] = dt; 
            GridView1.DataSource = dt;
        }
        else
        {
            dt = (DataTable)Session["TempTable"];
            GridView1.DataSource = dt;
        }
    }

    protected void AddButton_Click(object sender, EventArgs e)
    {
        dt = (DataTable)Session["TempTable"]; // Fetching datatable from session

        DataRow dr = dt.NewRow(); // Adding new row to datatable
        dr[0] = "Jenny";
        dr[1] = "LastName";
        dr[2] = "Jenny@hotmail.com";
        dr[3] = "867-5309";
        dt.Rows.Add(dr);

        Session["TempTable"] = dt;   // update datatable in session
        GridView1.DataSource = dt;   // updated datatable is now new datasource
        GridView1.DataBind();        // calling databind on gridview
    }

Question

Is there a property in GridView that I am not setting or is there something fundamentally wrong with my code? Any help toward a solution would be fantastic. Thanks in advance.

Solution

I made the suggested changes by Bart De Meyer to my AddButton_Click event and set the AutoGenerateColumns property of my GridView to true.

4

1 回答 1

3

您的 Click 事件将在页面加载事件之后执行。

在您的点击事件中:

  • 从会话中获取数据表
  • 将新行添加到数据表
  • 更新会话中的表
  • 将更新的数据表指定为 gridview 数据源
  • 在你的 gridview 上调用 DataBind

问题更新后。我试了一下:

private DataTable dt;

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        dt= new DataTable();

        dt.Columns.Add("First", typeof(String));
        dt.Columns.Add("Last", typeof(String));
        dt.Columns.Add("Email", typeof(String));
        dt.Columns.Add("Phone", typeof(String));
        Session["TempTable"] = dt;
        grid.DataSource = dt;
    }
    else
    {
        dt = (DataTable)Session["TempTable"];
        grid.DataSource = dt;
    }
}

protected void btnAdd_Click(object sender, EventArgs e)
{
    dt = (DataTable)Session["TempTable"]; // Fetching datatable from session

    DataRow dr = dt.NewRow(); // Adding new row to datatable
    dr[0] = "Jenny";
    dr[1] = "LastName";
    dr[2] = "Jenny@hotmail.com";
    dr[3] = "867-5309";
    dt.Rows.Add(dr);

    Session["TempTable"] = dt;   // update datatable in session
    grid.DataSource = dt;   // updated datatable is now new datasource
    grid.DataBind();        // calling databind on gridview
}

应该工作: 在此处输入图像描述

备注:Gridview ID参数=grid 按钮点击事件=btnAdd_Click

于 2013-04-17T12:09:10.543 回答