Tuesday, January 12, 2021

Custom delete in gridview in ASP.NET

 In the GridView, you should bind the file name using CommandArgument property. Like this:

            <asp:GridView ID="TaskGridView" runat="server"
                AllowPaging="true" AutoGenerateColumns="false"
                OnRowEditing="TaskGridView_RowEditing"
                OnRowCancelingEdit="TaskGridView_RowCancelingEdit"
                OnRowUpdating="TaskGridView_RowUpdating" OnRowDataBound="TaskGridView_RowDataBound"
                OnPageIndexChanging="TaskGridView_PageIndexChanging"
                OnRowCommand="TaskGridView_RowCommand" OnRowDeleting="TaskGridView_RowDeleting"
                >
                <Columns>
                    <asp:BoundField DataField="Id" Visible="false" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
                    <asp:BoundField DataField="Description" HeaderText="Description" ReadOnly="True" SortExpression="Description" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" CommandName="Delete" CommandArgument='<%# Eval("Description") %>' runat="server">Delete</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

Then in RowCommand event, you could refer to the following code:

        protected void TaskGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                string str = e.CommandArgument.ToString();
                //do something
            }
        }

No comments:

Post a Comment

No String Argument Constructor/Factory Method to Deserialize From String Value

  In this short article, we will cover in-depth the   JsonMappingException: no String-argument constructor/factory method to deserialize fro...