Gridview - populating dropdownlist in codebehind

Posted on 2007-10-15
Last Modified: 2010-02-11
hi Guys,
I am having a nightmare with this, i really hate using gridviews.
Anyway, basically i have a dropdown list in a gridview, which i want to bind to a datasource and populate it from codebehind (this allows me to populate it from a file listing).

I have tried to populate the list in just about all the gridview events, rowupdating, databound etc but as soon as i hit edit in the gridview i get an error stating the selectedvalue does not exist - thats because it is trying to set the selectedvalue (needed to bind it) before i can populate it.

So summary:

I click on the gridview edit button, which event do i use to populate the dropdown - without getting the
"SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value"

god this should be so simple.
Question by:Si_Hibbard
    LVL 9

    Expert Comment

    if your gridview has a autogenerated edit button then bind it in the row_editing event of the datagrid
    LVL 10

    Accepted Solution

    Found solution.

    First have to populate in the gridview databound event:

        protected void GridViewRowDataBound(object sender, GridViewRowEventArgs e)
            //GridView1.EditIndex = e.Row;

            //GridViewRow gvr = GridView1.Rows[GridView1.EditIndex];
            if (e.Row.RowType == DataControlRowType.DataRow)
                DropDownList ImageDropDown = (DropDownList)e.Row.FindControl("ImageDropDown");

                if (ImageDropDown != null)
                    String[] files = Directory.GetFiles(Server.MapPath("../RightSideImages/"));
                    String fileName;

                    //DropDownList ImageDropDown = ((DropDownList)GridView1.FindControl("ImageDropDown"));
                    //SmallImageList.Items.Add("-- Please Select --");

                    foreach (string path in files)
                        fileName = Path.GetFileName(path);

    Then have to catch the updating event and set the new value:

        protected void GridViewRowUpdating(object sender, GridViewUpdateEventArgs e)
            // value of template field has to be updated manually
            int iIdx = e.RowIndex;
            GridViewRow row = GridView1.Rows[iIdx];
            DropDownList ddl = (DropDownList)row.FindControl("ImageDropDown");
            e.NewValues["Filename"] = ddl.SelectedValue;
    LVL 1

    Expert Comment

    Closed, 500 points refunded.
    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    761 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

    11 Experts available now in Live!

    Get 1:1 Help Now