gridview行删除的简单例子

一点学习体会,留着自己看。

 直接拷贝到vs 2005下面,数据绑定后就可以运行了,[T_DB_Class] 是班级表,含有4个字段,分别为:

BJID:班级编号  TID:老师编号  StID:学生编号   班级名称:BJName,其中BJID主键,TID和 StID

是外键。

<%@ Page Language="C#" %>

<script runat="server">

    void CustomersGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e)
    {

        // Cancel the delete operation if the user attempts to remove
        // the last record from the GridView control.
        if (GridView1.Rows.Count <= 1)
        {

            e.Cancel = true;
            Message.Text = "You must keep at least one record.";

        }

    }

   
</script>

<html>
<body>
    <form id="Form1" runat="server">
        <h3>
            GridView RowDeleting Example</h3>
        <asp:Label ID="Message" ForeColor="Red" runat="server" />
        <br />
       
        <!-- The GridView control automatically sets the columns -->
        <!-- specified in the datakeynames property as read-only. -->
        <!-- No input controls are rendered for these columns in -->
        <!-- edit mode. -->
       
        <asp:GridView ID="GridView1" DataSourceID="tree1" AutoGenerateColumns="False"
            AutoGenerateDeleteButton="True" DataKeyNames="BJID" OnRowDeleting="CustomersGridView_RowDeleting"
            runat="server" AllowPaging="True" >
            <Columns>
                <asp:BoundField DataField="StID" HeaderText="StID" SortExpression="StID" />
                <asp:BoundField DataField="BJID" HeaderText="BJID" ReadOnly="True" SortExpression="BJID" />
                <asp:BoundField DataField="TID" HeaderText="TID" SortExpression="TID" />
                <asp:BoundField DataField="BJName" HeaderText="BJName" SortExpression="BJName" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="tree1" runat="server" ConnectionString="<%$ ConnectionStrings:classDBConnectionString2 %>"
            SelectCommand="SELECT * FROM [T_DB_Class]"
            DeleteCommand="Delete from [T_DB_Class] where BJID = @BJID">
        </asp:SqlDataSource>
        <!-- This example uses Microsoft SQL Server and connects --><!-- to the Northwind sample database. Use an ASP.NET --><!-- expression to retrieve the connection string value --><!-- from the Web.config file. -->
    </form>
</body>
</html>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值