如何删除网格视图的单个单元格时,在一次点击相关的单元格按钮

本文关键字:单元格 一次 按钮 视图 网格 删除 何删除 单个 | 更新日期: 2023-09-27 18:04:37

如何找到所选Linkbutton的列索引时,点击任何链接按钮是动态创建的。所有的按钮都是动态创建的,行和列也在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++;
    }
}

点击这里查看示例代码的截屏图像输出此处输入图像描述

如何删除网格视图的单个单元格时,在一次点击相关的单元格按钮

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

在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();
        }
    }
然而,你的问题是动态控制。当您添加动态控件时,您必须向按钮添加CommandNameCommandArgument,以便它像上面的代码片段中的按钮一样工作:
    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();
        }
    }
相关文章: