Thursday 27 June 2013

How to write code for Grid view RowDataBound Event ?

In this article I will explain what GridView RowDataBound Even is and how to bind data to the Grid view Dropdown list and Delete conformation message in ASP.NET

Grid view RowDataBound Event fired in page load. This below code is useful for bind data to the Grid view Dropdown list and Delete conformation message.

Gridview control is most common control in all asp.net applications to display the data and it’s very powerful control and lot of built in features. In this article, I will explore some tips and tricks in rowdatabound event in gridvew control, Actually RowDatabound event will occur when data row is bound to the control in the gridview control in ASP.NET

Syntax:

<asp:GridView OnRowDataBound="GridViewRowEventHandler" /> 

 Example:
 Below is the code for Grid view designing 

Design:

<asp:GridView ID="gvNew" runat="server" AutoGenerateColumns="false" OnRowCancelingEdit="RowCancelingEdit" OnRowCommand="RowCommand" OnRowDataBound="RowDataBound" OnRowEditing="RowEditing" OnRowUpdated="gvNew_RowUpdated" OnRowUpdating="RowUpdating" AllowPaging="true"  PageSize="30" DataKeyNames="ecm_id" PageIndexChanging="PageIndexChanging"
                            ShowFooter="false" OnRowDeleting="RowDeleting">
                            <Columns>
                                <asp:TemplateField HeaderStyle-Height="30px" FooterStyle-Height="25px">
                                    <ItemTemplate>
 <asp:Label runat="server" Text='<%# Eval("CategoryMain"%>' Width="100px" ID="lblCostCategory"></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:DropDownList ID="ddlCategoryMainET" runat="server" Width="100px">
                                        </asp:DropDownList>                                       
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:DropDownList ID="ddlCategoryMainFT" runat="server" Width="100px">
                                        </asp:DropDownList>
                                    </FooterTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Action" HeaderStyle-Height="30px" FooterStyle-Height="25px">
<ItemTemplate>
                                        <asp:Button Text="Edit" ID="Edit" runat="server" CommandArgument="<%# Container.DataItemIndex %>"
                                            CommandName="Edit" ToolTip="Edit" />&nbsp;&nbsp;<asp:Button Text="Delete" ID="btnDelete"
                                                runat="server" CommandArgument="<%# Container.DataItemIndex %>" CommandName="Delete"
                                                ToolTip="Delete" />
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:Button Text="Update" ID="Update" runat="server" CommandArgument="<%# Container.DataItemIndex %>"
                                            CommandName="Update" ToolTip="Update" ValidationGroup="Update" />&nbsp;&nbsp;<asp:Button
                                                Text="Cancel" ID="Cancel" runat="server" CommandArgument="<%# Container.DataItemIndex %>"
                                                CommandName="Cancel" ToolTip="Cancel" />
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:Button Text="ADD" ID="Insert" runat="server" CommandArgument="<%# Container.DataItemIndex %>"
                                            CommandName="Save" ToolTip="Insert" ValidationGroup="Save" />&nbsp;&nbsp;<asp:Button
                                                Text="Cancel" ID="Cancel" runat="server" CommandArgument="<%# Container.DataItemIndex %>"
                                                CommandName="Cancel" ToolTip="Cancel" />
                                    </FooterTemplate>
                                  
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>

Code:    Below is the code for Record delete message on grid view and Bind data to drop down list   in Grid view RowDataBound Event. Using this event we can able to find the specific controls such as dropdown and get values from control which is used inside gridview control.

protected void gvNew_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView drv = (DataRowView)e.Row.DataItem;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button btnDelete = (Button)e.Row.FindControl("btnDelete");
            if (btnDelete != null)
            {
                btnDelete.Attributes.Add("onclick""javascript:return " +
                    "confirm('Are you sure you want to delete this record?')");
            }

            DropDownList ddlCategoryMain = (DropDownList)e.Row.FindControl("ddlCategoryMainET");

            if (ddlCategoryMain != null)
            {
                DataSet dsCostCategory = new DataSet();
                dsCostCategory = ObjClass.GetCategory();
                ddlCategoryMain.DataSource = dsCostCategory.Tables[0];
                ddlCategoryMain.DataTextField = "CostCategoryDesc";
                ddlCategoryMain.DataValueField = "CostCategorySk";
                ddlCategoryMain.DataBind();

                if (dsCostCategory.Tables[0].Rows.Count > 0)
                {
                    ListItem item = new ListItem("--Select--""0");
                    ddlCategoryMain.Items.Insert(0, item);                   
                }        
// here ds is an DataSet( GridView data binding DataSet )
// 30 is an page size
           // Session["psAllocation"] is page index number       
                ddlCategoryMain.SelectedValue = ds.Tables[0].Rows[(Convert.ToInt32(Session["psAllocation"]) * 30) + e.Row.RowIndex]["CostCategorySk"].ToString();
            }
        }

    }


No comments:

Post a Comment