Tuesday, 18 March 2014

GridView rowdatabound event for dropdown in asp.net


In this article I will explain how to find inside GridView controls in RowDataBound Event in ASP.NET

Syntax:

<asp:GridView OnRowDataBound="GridViewRowEventHandler" />

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.

Find dropdown control in 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)
    {
DropDownList ddlMainET = (DropDownList)e.Row.FindControl("ddlMainET");
if (ddlMainET != null)
            {
                DataSet dsCostCategory = new DataSet();

                // here we get data from DataBase.

                dsCostCategory = ObjAtSite.GetCostCategory();     
                ddlMainET.DataSource = dsCostCategory.Tables[0];
                ddlMainET.DataTextField = "CostDesc";
                ddlMainET.DataValueField = "CostSk";
                ddlMainET.DataBind();

                if (dsECMCostCategory.Tables[0].Rows.Count > 0)
                {
                    ListItem item = new ListItem("--Select--", "0");
                    ddlMainET.Items.Insert(0, item);                   
                }

// here dsData is an DataSet( GridView data binding DataSet )
// 30 is an page size
// Session["psAllocation"] is page index number
ddlMainET.SelectedValue = dsData.Table[0].Rows[(Convert.ToInt32(Session["psAllocation"]) * 30) + e.Row.RowIndex]["CostSk"].ToString();
               
            }

}

No comments:

Post a Comment