Link to home
Start Free TrialLog in
Avatar of obb-taurus
obb-taurusFlag 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();
        }
    }
Avatar of ukerandi
ukerandi
Flag of United Kingdom of Great Britain and Northern Ireland image

protected void DPBind(ArrayList list)
     {
          list.Insert(0, "Choose Item");
          dropdownlist1.datasource = list;
          dropdownlist1.dataBind();
     }
<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
Avatar of ukerandi
ukerandi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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();
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.