C# ASP.NET-如何删除GridView中的一行?

C# ASP.NET-如何删除GridView中的一行?,c#,asp.net,gridview,linqdatasource,C#,Asp.net,Gridview,Linqdatasource,我希望当用户单击删除框时删除该行。使用行命令事件 单击GridView控件中的按钮时引发RowCommand事件。这使您能够提供一个事件处理方法,该方法在发生此事件时执行自定义例程 GridView控件中的按钮还可以调用控件的某些内置功能。要执行这些操作之一,请将按钮的CommandName属性设置为下表中的值之一 <asp:GridView ID="gridInboxMessage" runat="server" AllowPaging="True" AllowSorting=

我希望当用户单击删除框时删除该行。

使用行命令事件

单击GridView控件中的按钮时引发RowCommand事件。这使您能够提供一个事件处理方法,该方法在发生此事件时执行自定义例程

GridView控件中的按钮还可以调用控件的某些内置功能。要执行这些操作之一,请将按钮的CommandName属性设置为下表中的值之一

<asp:GridView  ID="gridInboxMessage" runat="server" AllowPaging="True"
    AllowSorting="True" AutoGenerateColumns="False" DataSourceID="LinqDataSource1"
    OnSelectedIndexChanged="gridInboxMessage_SelectedIndexChanged">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>                                        
                <asp:Button runat="server" ID="DeleteInbox" Text="delete" />
            </ItemTemplate>
        </asp:TemplateField>                                
        <asp:CommandField ShowSelectButton="True" SelectText="show text" />                                
        <asp:BoundField DataField="Title" HeaderText="title" ReadOnly="True" SortExpression="Title" />
        <asp:TemplateField SortExpression="Body" HeaderText="body">
            <ItemTemplate>
                <asp:Label ID="MyBody" runat="server" Text='<%# TruncateText(Eval("Body"))%>'>                            
                </asp:Label>
                <asp:Label ID="fullBodyRecieve" Visible="false" runat="server" Text='<%# Eval("Body")%>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField SortExpression="Sender" HeaderText="sender">
           <ItemTemplate>
               <asp:Label ID="sender" runat="server" Text='<%# GetCompanyNameById(Eval("Sender"))%>'>
               </asp:Label>
           </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField SortExpression="Date1" HeaderText="date">
           <ItemTemplate>
              <asp:Label ID="PersianDateRecieve" runat="server" Text='<%# GetPersianDate(Eval("Date1"))%>'>
              </asp:Label>
           </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <AlternatingRowStyle BackColor="orange" />
</asp:GridView>
<div id="contentBodyMessageRecieve" style="width:300px; border:1px silid black" runat="server">
</div>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="DataClassesDataContext" Select="new (Title, Body, Sender, Date1)" TableName="PrivateMessages" Where="Receptor == @Receptor">
    <WhereParameters>
        <asp:QueryStringParameter Name="Receptor" QueryStringField="idCompany" Type="String" />
    </WhereParameters>
</asp:LinqDataSource>
</fieldset>
<br />
<br />

到目前为止,您做了哪些工作,遇到了哪些问题?您是否更新了问题?如何感知当前标题字段“如何感知当前标题字段”?
 <asp:gridview id="ContactsGridView" 
          datasourceid="ContactsSource"
          allowpaging="true" 
          autogeneratecolumns="false"
          onrowcommand="ContactsGridView_RowCommand"
          runat="server">

          <columns>
            <asp:buttonfield buttontype="Link" 
              commandname="Delete" 
              text="Delete"/>
            <asp:boundfield datafield="ContactID" 
              headertext="Contact ID"/>
            <asp:boundfield datafield="FirstName" 
              headertext="First Name"/> 
            <asp:boundfield datafield="LastName" 
              headertext="Last Name"/>
          </columns>



        </asp:gridview>




Sub ContactsGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

' If multiple buttons are used in a GridView control, use the
' CommandName property to determine which button was clicked.
If e.CommandName = "Delete" Then

  ' Convert the row index stored in the CommandArgument
  ' property to an Integer.
  Dim index As Integer = Convert.ToInt32(e.CommandArgument)

      'Call you delete function here 
    End IF      
 End Sub**strong text**