【问题标题】:how to delete single cell of gridview at a time when click on related cell button单击相关单元格按钮时如何一次删除gridview的单个单元格
【发布时间】:2016-08-30 10:56:04
【问题描述】:

当单击任何动态创建的链接按钮时,如何动态找到所选链接按钮的列索引。所有按钮都是动态创建的,并且行和列也是在 gridview 中动态创建的。单击相关的链接按钮时,我需要删除gridview的单个单元格。所以我需要找到列和行索引。这个屏幕截图给出了问题的想法。

此代码用于在列不固定的情况下动态创建链接按钮。当单击gridview的相关单元格的删除按钮时,我需要删除单个单元格。

protected void gv_TT_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[2].Visible = false;
            e.Row.Cells[1].Attributes.Add("colspan", "2");
        }
        int index = e.Row.RowIndex;
        int k = 0;
        GVdata = (DataTable)ViewState["GVdata"];
        GridViewTable = (DataTable)ViewState["GridViewTable"];

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < GVdata.Rows.Count; i++)
        {

            // ADD LINK BUTTON IF row.text != Empty(Blank)
            if (e.Row.Cells[i + 3].ToString() != "&nbsp;")
            {
                if (e.Row.Cells[i + 3].ToString() != " ")
                {
                    if (e.Row.Cells[i + 3].ToString() != string.Empty)
                    {
                        Label txt = new Label();

                        LinkButton lb1 = new LinkButton();
                        lb1.ID = k + "_" + (i + 3) + "Btn1";
                        lb1.Text = " Update ";
                        lb1.ForeColor = System.Drawing.Color.Blue;

                        LinkButton lb2 = new LinkButton();
                        lb2.ID = k + "_" + (i + 3) + "Btn2";
                        lb2.Text = " Delete ";
                        lb2.ForeColor = System.Drawing.Color.Red;

                        lb1.CausesValidation = false;
                        lb1.Click += new EventHandler(Update_Click);
                        lb2.CausesValidation = false;
                        lb2.Click += new EventHandler(Delete_Click);

                        txt.Text = GridViewTable.Rows[index][i + 3].ToString();
                        e.Row.Cells[i + 3].Controls.Add(txt);
                        e.Row.Cells[i + 3].Controls.Add(lb1);
                        e.Row.Cells[i + 3].Controls.Add(lb2);
                    }
                }
            }
        }
        k++;
    }
}

Click here for Screen Shot image output of Sample Code enter image description here

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    您可以这样做从网格中删除一行。

    在 GridView 模板中:

    <asp:Button ID="Button1" runat="server" Text="Delete" CommandName="delRow" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "itemID").ToString() %>' />
    

    然后在RowCommand后面的代码中

        protected void gv_TT_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "delRow")
            {
                //get the id from the button and covert it
                int itemID = Convert.ToInt32(e.CommandArgument);
    
                //cast the viewstate as a datatable
                DataTable dt = ViewState["GridViewTable"] as DataTable;
    
                //filter out the row with the itemID to be deleted with Linq
                DataTable dtFiltered = dt.AsEnumerable().Where(row => row.Field<int>("itemID") != itemID).CopyToDataTable();
    
                //set the viewstate with the filtered datatable
                ViewState["GridViewTable"] = dtFiltered;
    
                //rebind the grid
                GridView1.DataSource = dtFiltered;
                GridView1.DataBind();
            }
        }
    

    但是,您的问题是动态控件。添加动态控件时,您必须在按钮上添加一个CommandName 和一个CommandArgument,它才能像上面sn-p 中的按钮一样工作:

        protected void gv_TT_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lb1 = new LinkButton();
                lb1.ID = "Btn1";
                lb1.Text = "DeleteRow";
                lb1.CommandName = "delRow";
                //value cannot be assigned yet
                //lb1.CommandArgument = "";
                lb1.ForeColor = System.Drawing.Color.Blue;
    
                e.Row.Cells[1].Controls.Add(lb1);
            }
        }
    

    只有一个问题。为了分配CommandArgument,我们必须使用GridView 中的数据,但RowCreated 中还没有数据。所以我们必须在RowDataBound事件中赋值:

        protected void gv_TT_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = e.Row.DataItem as DataRowView;
                LinkButton lb1 = e.Row.FindControl("Btn1") as LinkButton;
                lb1.CommandArgument = drv["itemID"].ToString();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-21
      • 1970-01-01
      相关资源
      最近更新 更多