How to programmatically bind data to a dropdown list (combobox) in a gridview control for ASP 2.0

Posted on 2006-03-30
Last Modified: 2008-01-09
How can I programmatically bind data to a dropdown list (combobox) in a gridview control for ASP 2.0. I have a dataset that I am using to bind data to a gridview control that has all columns as labels. I am using the following code to bind my data:

'This method gets me the data to the TVData datatable variable

TVData = objSQL.GetHeader(ClientCode, DivisionCode, Market, Year, Month, MediaType)

'Here I am binding my datatable to my gridview control            

gvTV.DataSource = TVData

As a  result I see all my label columns in the gridview populated with the data. However my column containing the combo box is not populated. Any ideas or code, is there any event where you have to write that code?

Thanks in advance
Question by:smkkaleem
    LVL 10

    Expert Comment


    You need to set few fields in combobox so it gets populated, something like this:

    <asp:TemplateColumn HeaderText="HeaderText">
              <asp:DropDownList runat="server" id="cmbID" AutoPostBack="true" OnSelectedIndexChanged="cmb_SelectedIndexChanged" DataSource='<%# GetCmbDataSource() %>' DataTextField="cmbTextField" DataValueField="cmbValueField" >

    and of course implement in codebehind "GetCmbDataSource "


    Look here:
    LVL 2

    Author Comment

    I  tried to follow the first link, does not work. The second link talks about the datgrid control for VS 1.1.  My question is regarding the gridview control in ASP.NET 2.0. Any more examples?

    LVL 10

    Accepted Solution

    LVL 3

    Expert Comment


    I think what you need to do is to  grab that control on the Item Creadted event of  the gridview or datagrid. Make sure that you get it while it is not created in the header or the footer. Once you find it you can bind to it in the event.
    It will be something like this:

     private void  R_ItemCreated(object sender, System.Web.UI.WebControls.XXXXItemEventArgs e)

                      if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
                                  lstFacility = (DropDownList) e.Item.FindControl("ddFacility");
                                  lstFacility.DataSource = htFacility;
                                  lstFacility.DataTextField = "value";
                                  lstFacility.DataValueField = "key";

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now