j1473227713 2021-12-02 21:58 采纳率: 50%
浏览 36
已结题

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后面自动添加一列 内容是删除 然后这个功能要怎么实现?

  • 写回答

1条回答 默认 最新

  • CSDN专家-showbo 2021-12-02 22:18
    关注
    <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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月10日
  • 已采纳回答 12月2日
  • 创建了问题 12月2日

悬赏问题

  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真