日期:2014-05-17  浏览次数:20456 次

gridview删除某一行
<asp:GridView ID="gvMessageList" runat="server" AllowPaging="True" 
                    AutoGenerateColumns="False" 
                    onpageindexchanging="gvMessageList_PageIndexChanging" 
                    DataKeyNames="MessageId" onrowdeleting="gvMessageList_RowDeleting" >
                    <Columns>
                        <asp:BoundField DataField="MessageId" HeaderText="编号" />
                        <asp:BoundField DataField="Title" HeaderText="标题" />
                        <asp:BoundField DataField="Message1" HeaderText="留言内容" />
                        <asp:BoundField DataField="CreateDate" HeaderText="留言日期" />
                        <asp:BoundField DataField="IP" HeaderText="IP" />
                        <asp:BoundField DataField="Email" HeaderText="邮箱" />
                        <asp:BoundField DataField="UserName" HeaderText="留言用户" />
                        <asp:CommandField ShowDeleteButton="True" />
                    </Columns>
                </asp:GridView>



 protected void gvMessageList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int key =Convert.ToInt32(gvMessageList.DataKeys[e.RowIndex]["MessageId"].ToString());
        var gvML = from c in db.Message
                   where c.MessageId ==key
                   select c;
        foreach(var c in gvML)
        {
           db.Message.DeleteOnSubmit(c);
        }
        db.SubmitChanges();