从数据表中删除行并显示更新的gridview

时间:2016-09-14 11:03:17

标签: c# asp.net gridview datatable

从下拉列表中添加数据表中的数据并将其显示在gridview中。然后我删除一行并维护ViewState但是在删除一行并再次映射我的值之后,我在gridview中获得了具有重复记录的表的早期状态。

以下是代码:

DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        //dt = new DataTable("tblTest");
        //dt = ViewState["updatedtbl"] as DataTable;
        DataColumn dc1 = new DataColumn();
        dc1.DataType = typeof(String);
        dc1.ColumnName = "A";
        DataColumn dc2 = new DataColumn();
        dc2.DataType = typeof(String);
        dc2.ColumnName = "B";
        dt.Columns.Add(dc1);
        dt.Columns.Add(dc2);
        //ViewState["dt"] = dt;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            if (Path.GetExtension(FileUpload1.FileName) == ".xlsx")
            {
                DataTable dt = new DataTable();
                ExcelPackage package = new ExcelPackage(FileUpload1.FileContent);
                dt = package.ToDataTable();

                DropDownList1.Items.Clear();

                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    DropDownList1.Items.Add(new ListItem(dt.Columns[i].ColumnName));
                }
            }
        }
    }

    protected void Button3_Click(object sender, EventArgs e)
    {

        Session["A"] += DropDownList1.SelectedItem.Value + "|";
        Session["B"] += DropDownList2.SelectedItem.Value + "|";
        CreateTable();
    }

    public void CreateTable()
    {
        string[] sa = Session["A"].ToString().Split('|');
        string[] sb = Session["B"].ToString().Split('|');
        int recordnum = sa.Length;
        for (int j = 0; j < recordnum - 1; j++)
        {

            DataRow dr = dt.NewRow();
            dr["A"] = sa[j].ToString();
            dr["B"] = sb[j].ToString();
            dt.Rows.Add(dr);
        }
        ViewState["tbl"] = dt;
        BindGrid();
    }

    protected void BindGrid()
    {
        GridView2.DataSource = dt;
        GridView2.DataBind();
    }

    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DataTable dx = ViewState["tbl"] as DataTable;
        //DataTable dx = dt;
        if (dx.Rows.Count > 0)
        {
            dx.Rows[e.RowIndex].Delete();
            dx.AcceptChanges();

            GridView2.DataSource = dx;
            GridView2.DataBind();
            ViewState["updatedtbl"] = dx;

        }
        else       //To check which portion of code is being executed
        {
            Label1.Text = "Deleted";
            Label2.Text = dx.Rows.Count.ToString();
        }
    }

1 个答案:

答案 0 :(得分:0)

在删除事件中,您将更新的数据表重新分配给名为updatedtbl的不同视图状态。您应该将其分配给ViewState["tbl"]

if(dx.Rows.Count > 0)
        {
            dx.Rows[e.RowIndex].Delete();
            dx.AcceptChanges();

            GridView2.DataSource = dx;
            GridView2.DataBind();
            //ViewState["updatedtbl"] = dx; <<---- pay attention here
            ViewState["tbl"] = dx; <<---- pay attention here          
        }