How to use rowcommand in Asp.net gridview control

How to use RowCommand in Asp.net gridview control:



Asp.net Grid view control:


As we know that the gridview is more useful and powerful control in asp.net for display g
Gridview displays the value of a data source in a table. Here, I am going to give an example of using an event called "RowCommand". The RowCommand is raised when a button, LinkButton or ImageButton is clicked in the Gridview Control.data in tabular form.

he 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.

First we create a grid view with rowcomand event:


<asp:GridView ID="Grdcat" runat="server" AutoGenerateColumns="False" CellPadding="4"
                    Width="50%" EmptyDataRowStyle-Font-Bold="true" EmptyDataRowStyle-ForeColor="Red"
                    EmptyDataText="There is no Data." ForeColor="#333333" Height="196px" OnRowCommand="Grdcat_RowCommand"
                    OnRowDeleting="Grdcat_RowDeleting">
                    <RowStyle BackColor="White" />
                    <EmptyDataRowStyle Font-Bold="True" ForeColor="Red"></EmptyDataRowStyle>
                    <Columns>
                        <asp:TemplateField HeaderText="SNo" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <%#Container.DataItemIndex+1 %>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:TemplateField>
                        <asp:TemplateField ItemStyle-HorizontalAlign="Center" Visible="false">
                            <ItemTemplate>
                                <asp:Label ID="lblcatgryId" runat="server" Text='<%# Eval("catgryId") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Name" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:Label ID="lblcatgryName" runat="server" Text='<%# Eval("catgryName") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:TemplateField>
                        <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkshow" runat="server" CommandName="Delete" Font-Size="Larger"
                                    ForeColor="Brown" CommandArgument='<%#DataBinder.Eval(Container, "RowIndex")%>'
                                    Text="Delete"></asp:LinkButton>
                            </ItemTemplate>

<ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:TemplateField>
                      
                    </Columns>
                    <FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True" />
                    <HeaderStyle BackColor="#0033cc" Font-Bold="True" ForeColor="White" Height="40px" />
                    <EditRowStyle BackColor="#7C6F57" />
                </asp:GridView>

The C# code bihaind this grid view control:

protected void Grdcat_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            int index = Convert.ToInt32(e.CommandArgument.ToString());
            Label ltrlslno = (Label)Grdcat.Rows[index].FindControl("lblcatgryId");
            // write code for use this value as you see here i want to delete this particular
            //item from data base then i need to write delete sql query here.
        }
    }
    protected void Grdcat_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }



Asp.net Related Other Post:

Comments

Popular posts from this blog