Wednesday, 4 January 2012

Select Gridview and do edit or delete by the help of gridview_rowcommand event

 // Gridview Edit by the help of link button //
<asp:GridView ID="GridViewPartyTypeDetails" runat="server" AutoGenerateColumns="False"
                OnRowCommand="GridViewProjects_RowCommand" CssClass="GridViewDefault" AllowPaging="True"
                OnPageIndexChanging="GridViewPartyTypeDetails_PageIndexChanging">
                <Columns>
                    <asp:TemplateField HeaderText="PartyType Name">
                        <ItemTemplate>
                            <asp:LinkButton ID="LnkBtnPartyType" runat="server" CommandName="Edit" Text='<%# Eval("PartyTypeName") %>'
                                CommandArgument='<%#Eval("PartyTypeID") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EmptyDataTemplate>
                    <asp:Label ID="LblEmptyMessage" runat="server" Font-Bold="False" Text="No Record Found." />
                </EmptyDataTemplate>
            </asp:GridView>
C# Code
 protected void GridViewProjects_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
             Response.Redirect("PartyTypeEntry.aspx?PtId=" + e.CommandArgument.ToString());
             }

            if (e.CommandName == "Del")
            {
                Satya.GetInstance.DeleteName(int.Parse(e.CommandArgument.ToString()));
                Label lb = (Label)this.Master.FindControl("lblMsg");
                lb.Text = "Data Deleted Successfully !";
                TxtSearch.Text = "";
                FillGridViewProjects();
            }
        }

if you want to change the gridview page index changing this code
 protected void GridViewPartyTypeDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewPartyTypeDetails.PageIndex = e.NewPageIndex;
            FillGridViewProjects();
        }

No comments:

Post a Comment