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

obb-taurus
obb-taurus used Ask the Experts™
on
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();
        }
    }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

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

Commented:
<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>
Commented:
ddlList.DataSource = dsDataSource
ddlList.DataBind()
ddlList.Items.Insert(0,"Choose any one")
ddlList.SelectedIndex = 0
Kamal KhaleefaInformation Security Specialist

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial