在GridView的最后一列中添加删除按钮

在GridView的最后一列中添加删除按钮

本文介绍了在GridView的最后一列中添加删除按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网​​格视图动态

DataTable dt1 = new DataTable();
dt1.Columns.Add("Item Title", typeof(string));
dt1.Columns.Add("Unit Pack", typeof(string));
dt1.Columns.Add("Pack", typeof(string));
gv1.DataSource = dt1;
gv1.DataBind();

和增加自动删除按钮

<asp:GridView ID="gv" runat="server">
    <Columns>
        <asp:CommandField ShowDeleteButton="true" />
    </Columns>
</asp:GridView>

它显示在GridView像

It shows the gridview like

| Delete | Item Title | Unit Pack | Pack |

现在我要显示在最后一列像

Now i want to Show Delete button at the last column like

| Item Title | Unit Pack | Pack | Delete |

我怎样才能做到这一点?如何在最后一栏创建删除按钮?

How can I do this? How to create delete button at last column?

推荐答案

您添加列动态这就是为什么你需要添加删除列的动态。或者你可以在添加的RowDataBound LinkBut​​ton的:

You are adding column dynamically thats why you need to add delete column dynamic. Or you can add linkButton in RowDataBound:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem != null)
{
    LinkButton lb = new LinkButton();
    lb.CommandArgument = e.Row.Cells[3].Text;
    lb.CommandName = "Delete";
    lb.Text = "Delete";

    e.Row.Cells[3].Controls.Add((Control)lb);
}
}

在rowCommand你可以写code进行删除:

in rowCommand you can write code for delete:

protected void gv_RowCommand(object sender, CommandEventArgs e)
{
switch (e.CommandName.ToLower())
{
    case "delete":
//your code here
        break;
    default:
        break;
}
}

这篇关于在GridView的最后一列中添加删除按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 19:10