Binding DropDownList asp.net 2.0 control which is placed in the GridView control is very easy and many of you know how to bind the DropDownList which is placed in the GridView Control.But there may some people who are beginner to the web development and didn't know how to bind the DropDownList which is placed in the GridView control.
For this code example which is simple one i have added one GridView Control in the main form. The GridView has only two columns one is to display the name of the order and the second which is used to show the status of the Order and I have place the DropDownList control for the status of the order. In the Page_Load event of the form you can bind the the GridView control to the what ever your data source. And then you can bind the DropDownList control in the RowDataBound Event of the GridView. Here is the code of the RowDataBound event of the GridView control.
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlDropDownList = (DropDownList)e.Row.FindControl("ddlDropDownList");
if (ddlDropDownList != null)
{
ddlDropDownList.DataSource = GetOrderStatus();
ddlDropDownList.DataTextField = "Status";
ddlDropDownList.DataValueField = "StatusID";
ddlDropDownList.DataBind();
}
}
In the above code of the GridView RowDataBound event what i did is the to check the RowType of the current Data Bound Row, Here i place the DataRow type check so that i can only perform the action on the DataRow type rows. Next after the if condition i have search for the DropDownList control in the current row by using the FindControl function of the current row and passing the name of the DropDownList control. After the i have place the if condition to check where DropDownList control is found or not.If found then ,set the DataSource of the DropDownList control and set the DataTextField and DataValueField of the DropDownList control and at the end call the DataBind of the DropDownList control.
The DataControlRowType enumeration identifies the function of rows in a data control. It is used by the DetailsView and GridView controls to distinguish between rows that display data and rows that display other user interface (UI) elements, such as a header row, row separator, or pager buttons.
You can use the DataControlRowType enumeration to identify the type of a GridViewRow or DetailsViewRow object when you enumerate through a GridViewRowCollection or DetailsViewRowCollection collection. If you are writing a data control that creates rows, you can use the DataControlRowType enumeration to identify the function of different rows in the control.
Note: I have used the DataTable which is created in the code and i have set the status and statusID of the data table you can replace your Column names which status and statusID.
You can download the source code from hereFor user here is the complete list of the DataControlRowType named constant.
Member Name | Description |
---|---|
Header | A header row of a data control. Header rows cannot be data-bound. |
Footer | A footer row of a data control. Footer rows cannot be data-bound. |
DataRow | A data row of a data control. Only DataRow rows can be data-bound. |
Separator | A row separator. Row separators cannot be data-bound. |
Pager | A row that displays pager buttons or a pager control. Pager rows cannot be data-bound. |
EmptyDataRow | The empty row of a data-bound control. The empty row is displayed when the data-bound control has no records to display and the EmptyDataTemplate template is not null reference ( Nothing in Visual Basic). |
You can use the DataControlRowType enumeration to identify the type of a GridViewRow or DetailsViewRow object when you enumerate through a GridViewRowCollection or DetailsViewRowCollection collection. If you are writing a data control that creates rows, you can use the DataControlRowType enumeration to identify the function of different rows in the control.
Note: I have used the DataTable which is created in the code and i have set the status and statusID of the data table you can replace your Column names which status and statusID.
All and any comments / bugs / suggestions are welcomed!
5 comments:
Thanks alot its great help to understand the binding differrent web contents with the grid view.
Keep it uppp
Anonymous: thanks for your appreciation and your valuable time for posting your valuable comments.
I would like to invite to new programming resource website www.codegain.com
RRaveen
Founder
The Codegain.com
Dear this code is working when i take dropdown in itemtemplate code
but when i m taking dropdrown in footer row then it is prodecing erorr object reference is not set an instance...
code is given below
DropDownList ddlDropDownList = (DropDownList) .grdvCategoryList .FooterRow.FindControl("ddlDropDownList");//erorr in this line
ddlDropDownList.DataSource = ds.Tables[0];
ddlDropDownList.DataTextField = "statename";
ddlDropDownList.DataValueField = "statename";
ddlDropDownList.DataBind();
...plz send answer.thnks
Samarjeet: See my post to get your answer
http://asimsajjad.blogspot.com/2009/09/adding-and-binding-dropdownlist-control.html
Hope that will help and solve your problem.
Post a Comment