Populate Dropdownlist in Gridview using function

Posted on 2011-05-11
Last Modified: 2012-06-21
I have a function that I use to populate dropdownlist on web forms with values from a SQL database.   I have a dropdown list inside of a gridview that is in a template column that i want to populate using that same function.  I think i have done this before but can figure it out.  Any help would be appreciated
Question by:Mike_Stevens
    LVL 83

    Expert Comment

    Use the RowDataBound event
    LVL 7

    Expert Comment

    You'll have to do this inside of the gridview.OnRowDataBound event, like this...

    protected void gvYourGridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
    	//Check to make sure the databound row is one of the data rows and not the header
    	if (e.Row.RowType == DataControlRowType.DataRow)
    		DropDownList ddl = e.Row.FindControl("IdOfYourDropDownListControlInTheTemplateColumn") as DropDownList;
    		//Run your data function on the ddl

    Open in new window

    LVL 13

    Accepted Solution

    Here's a basic example:
    Sub PopulateGridview()
    dim drp as dropdownlist = gridview1.rows(1).Cells(1).findcontrol("dropdownlist1")
    Dim cnn As New SqlConnection(ConnString)
    Dim reader As SqlDataReader
    Dim cmd As SqlCommand = New SqlCommand("select column from table", cnn)
    reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    While reader.Read 
    End While
    End Sub

    Open in new window

    LVL 13

    Expert Comment

    I forgot... you need to have that inside a for each loop:

       for each gridviewrow In gridview1.rows

    Or use the gridview's RowDataBound event, like like CodeCruiser and JosephEricDaves mentioned.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    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…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    758 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

    13 Experts available now in Live!

    Get 1:1 Help Now