How to append a row to an existing drop down list so the user sees None and the selected value is 0 as the first choice.

Posted on 2004-11-11
Last Modified: 2010-04-24
Greetings experts.

I am loading a drop down list using the following procedure. My question is how do I add an entry to the ddlHourDrop below so that the first choice in the list is None in the datatextfield property and 0 in the datavaluefield property without None and 0 actually being a row in the source table  PS_Hours (see stored procedure below).



   Private Sub HourAdd_ddl()
        ' This code loads the hour drop-down list used when adding a time record
        Dim cmdSelect As SqlCommand
        Dim dtrHour As SqlDataReader
        cmdSelect = New SqlCommand("PS_HourDrop", conPS)
        cmdSelect.CommandType = CommandType.StoredProcedure
        dtrHour = cmdSelect.ExecuteReader()
        ddlHourDrop.DataSource = dtrHour
        ddlHourDrop.DataTextField = "HourDesc"
        ddlHourDrop.DataValueField = "Hourid"
    End Sub

The following is the stored procedure that loads the ddl.

SELECT HourDesc, Hourid
From PS_Hours
Question by:WRead
    LVL 28

    Expert Comment

    before populating the drop down add an empty line

    after that populate from your SP

    and then DropDown1.SelectedIndex = 0

    Author Comment

    Thanks for the suggestion above. However, when I add the following line of code you suggested
    ' DropDownList1.Items.Add("") ' before I populate the ddl with my SP, the new item is not inserted into the ddl.
    I tried doing what you suggested without calling the SP and the new blank row does in fact get inserted into the ddl. I still have the original problem which is I can't populate the ddl manually(as you suggested) AND from the SP. It is as if the SP is replacing/overriding the added item. Is this possible? Recall the following line in my procedure above
    ddlHourDrop.DataSource = dtrHour. I think this line says that the datasource for my ddl is the datareader named dtrHour.
    Does this line overwrite your items add line of code?

    Could you please be more specific with regard to exactly where I should insert these two lines of code with regard to my vb procedure above? FYI, Following your instructions I inserted  DropDownList1.Items.Add("") at the very top of my procedure named HourAdd_ddl(). Next I inserted DropDown1.SelectedIndex = 0 just before the folllowing databind
    statement  ddlHourDrop.DataBind() that is just before the ConPS.close() statement. Thanks again for your suggestion.
    Any additional ideas?

    LVL 20

    Expert Comment

    After bind insert a new item to the dropdownlist at the first position

            ddlHourDrop.DataSource = dtrHour
            ddlHourDrop.DataTextField = "HourDesc"
            ddlHourDrop.DataValueField = "Hourid"
            ddlHourDrop.Insert( 0, "None" )
    LVL 14

    Expert Comment

    How about setting the SelectedItem value to -1 after data binding?
    LVL 2

    Accepted Solution

    I'll recommend to add new row with the parameters that you want to show, to the table returning form the sp, and then databind.....


    DataTable tab1 = DataLayer.GetTable();  // i'm guessing that gettable returns a table based on a sp.
    DataRow r1 = tab1.NewRow();
    r1["ID"] = 0;
    r1["name"] = "None";      // I'm guessing that the table has a columnd ID and a column None, and one is the Display Value  
                                         //and the other is the Value Member

    comboBox.DataSource = tab1;
    comboBox.DisplayMember = "name";
    comboBox.ValueMember = "ID";

    comboBox.SelectedValue = 0;
    hope this helps...
    LVL 20

    Assisted Solution

    Sorry I didn't read the q carefully, to make the text to "none" and value to 0

            ddlHourDrop.DataSource = dtrHour
            ddlHourDrop.DataTextField = "HourDesc"
            ddlHourDrop.DataValueField = "Hourid"
            ddlHourDrop.Insert( 0, New ListItem("None", "0") )

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    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…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    729 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

    19 Experts available now in Live!

    Get 1:1 Help Now