Avatar of obb-taurus
obb-taurus
Flag for Canada asked on

How to show a blank for first item in drop down list

I have a few drop down lists in my ASP.NET web form and would like them not to display the first entry of the data that's used to populate the control, I would like it to be either blank or that it says something like "Choose Item".  The code I use to load and bind the controls is as follows:

cboCategoryList.DataSource = database.GetInventoryCategories();
cboCategoryList.DataValueField = "ID";
cboCategoryList.DataTextField = "Description";
cboCategoryList.DataBind();

 public DataView GetInventoryCategories()
    {

        string selectStatement =
            "SELECT ID, Description " +
            "FROM InventoryCategories " +
            "ORDER BY ID";
        SqlDataAdapter da = new SqlDataAdapter(selectStatement, _connectionString);
        DataTable dt = new DataTable();

        try
        {
            da.Fill(dt);
            return dt.DefaultView;
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        finally
        {
            _connectionString.Close();
        }
    }
ASP.NETC#

Avatar of undefined
Last Comment
Vishal Kedar

8/22/2022 - Mon
ukerandi

protected void DPBind(ArrayList list)
     {
          list.Insert(0, "Choose Item");
          dropdownlist1.datasource = list;
          dropdownlist1.dataBind();
     }
ukerandi

<asp:dropdownlistID="Dropdownlist1"runat="server"AutoPostBack="True"OnSelectedIndexChanged="Dropdownlist1_SelectedIndexChanged">

<asp:ListItemvalue="0">Choose Item</asp:ListItem>

<asp:ListItemvalue="1">1</asp:ListItem>
<asp:ListItemvalue="2">2</asp:ListItem>

</asp:dropdownlist>
ASKER CERTIFIED SOLUTION
ukerandi

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Kamal Khaleefa

try to add this
dim dt as new datattable
dt=database.GetInventoryCategories();

Dim dr As DataRow = dt.NewRow
        dr("ID") = "-1"
        dr("Description") = "please select "
        dt.Rows.InsertAt(dr, 0)

cboCategoryList.DataSource = dt
cboCategoryList.DataValueField = "ID";
cboCategoryList.DataTextField = "Description";
cboCategoryList.DataBind();
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Vishal Kedar

try below code.

try
        {
            da.Fill(dt);
            DataRow dr = dt.NewRow;
            dr("ID") = "0";
           dr("Description") = " ";  //  for Blank    dr("Description") = "--Select-- ";  for some text.

           dt.Rows.InsertAt(dr, 0);

            return dt.DefaultView;
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        finally
        {
            _connectionString.Close();
        }
    }

It may helps.