asp.net GridView绑定数据库后,怎么动态添加一列,内容是删除并实现删除改行?

前台代码:

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Font-Size="20px" Width="600px">
            
                <AlternatingRowStyle BackColor="White"></AlternatingRowStyle>

                <EditRowStyle BackColor="#2461BF"></EditRowStyle>

                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"></FooterStyle>

                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Left"></HeaderStyle>

                <PagerStyle HorizontalAlign="Center" BackColor="#2461BF" ForeColor="White" ></PagerStyle>

                <RowStyle BackColor="#EFF3FB" Height="30px" HorizontalAlign="Left"></RowStyle>

                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333"></SelectedRowStyle>

                <SortedAscendingCellStyle BackColor="#F5F7FB"></SortedAscendingCellStyle>

                <SortedAscendingHeaderStyle BackColor="#6D95E1"></SortedAscendingHeaderStyle>

                <SortedDescendingCellStyle BackColor="#E9EBEF"></SortedDescendingCellStyle>

                <SortedDescendingHeaderStyle BackColor="#4870BE"></SortedDescendingHeaderStyle>
            </asp:GridView>

后台代码:

string sqlconstr = ConfigurationManager.ConnectionStrings["CS"].ConnectionString;
        SqlConnection sqlconn = new SqlConnection(sqlconstr);
        SqlCommand command = new SqlCommand();
        command.Connection = sqlconn;
        sqlconn.Open();
        command.CommandText = "SELECT ROW_NUMBER() OVER(ORDER BY o.time DESC) id, o.name,o.price,o.time from [order] o,[User] u where o.uid=u.id";
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataSet dataset = new DataSet();
        adapter.Fill(dataset);
        if (dataset.Tables[0].Rows.Count > 0){
            GridView1.DataSource = dataset;
            GridView1.DataBind();
        }else{
            orderTip.Text = "您没有订单记录~";
            //MessageBox.Show("没有相关记录");
            
        }
 adapter.Dispose();
            dataset.Dispose();
            command.Dispose();
            sqlconn.Close();
            sqlconn.Dispose();

效果:

img


就是在time后面自动添加一列 内容是删除 然后这个功能要怎么实现?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="job_id"
    OnRowCommand="GridView1_RowCommand" >
    <Columns>
        <asp:BoundField DataField="job_id" />
        <asp:BoundField DataField="max_lvl" />
        <asp:BoundField DataField="min_lvl" />
        <asp:ButtonField ButtonType="Button" CommandName="aa" Text="删除" />
    </Columns>
</asp:GridView>

在GridView里面添加个ButtonField 类型是Button的 ,还有关联事件名要起一个名字,因为后面删除要用到这个

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "aa")//aa是关联的事件名
    {
        int index = Convert.ToInt32(e.CommandArgument);//获取命令的参数
        int iLeagueID = Convert.ToInt32(this.GridView1.DataKeys[index].Value);//获取当前点击列的ID号
        Response.Write( "<script>alert('" + iLeagueID + "')</script>");
    }
}

————————————————
版权声明:本文为CSDN博主「sandykwx」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sandykwx/article/details/8511878