Search code examples
c#asp.netgridviewlinqdatasource

ASP.NET - How do I delete a row of a GridView?


<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 />

i want when user click on DeleteBox delete that row.


Solution

  • Use RowCommand Event.

    The RowCommand event is raised when a button is clicked in the GridView control. This enables you to provide an event-handling method that performs a custom routine whenever this event occurs.

    Buttons within a GridView control can also invoke some of the built-in functionality of the control. To perform one of these operations, set the CommandName property of a button to one of the values in the following table.

     <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**
    

    GridView row command