obb-taurus
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.GetInventoryCateg ories();
cboCategoryList.DataValueF ield = "ID";
cboCategoryList.DataTextFi eld = "Description";
cboCategoryList.DataBind() ;
public DataView GetInventoryCategories()
{
string selectStatement =
"SELECT ID, Description " +
"FROM InventoryCategories " +
"ORDER BY ID";
SqlDataAdapter da = new SqlDataAdapter(selectState ment, _connectionString);
DataTable dt = new DataTable();
try
{
da.Fill(dt);
return dt.DefaultView;
}
catch (SqlException ex)
{
throw ex;
}
finally
{
_connectionString.Close();
}
}
cboCategoryList.DataSource
cboCategoryList.DataValueF
cboCategoryList.DataTextFi
cboCategoryList.DataBind()
public DataView GetInventoryCategories()
{
string selectStatement =
"SELECT ID, Description " +
"FROM InventoryCategories " +
"ORDER BY ID";
SqlDataAdapter da = new SqlDataAdapter(selectState
DataTable dt = new DataTable();
try
{
da.Fill(dt);
return dt.DefaultView;
}
catch (SqlException ex)
{
throw ex;
}
finally
{
_connectionString.Close();
}
}
<asp:dropdownlistID="Dropd ownlist1"r unat="serv er"AutoPos tBack="Tru e"OnSelect edIndexCha nged="Drop downlist1_ SelectedIn dexChanged ">
<asp:ListItemvalue="0">Cho ose Item</asp:ListItem>
<asp:ListItemvalue="1">1</ asp:ListIt em>
<asp:ListItemvalue="2">2</ asp:ListIt em>
</asp:dropdownlist>
<asp:ListItemvalue="0">Cho
<asp:ListItemvalue="1">1</
<asp:ListItemvalue="2">2</
</asp:dropdownlist>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try to add this
dim dt as new datattable
dt=database.GetInventoryCa tegories() ;
Dim dr As DataRow = dt.NewRow
dr("ID") = "-1"
dr("Description") = "please select "
dt.Rows.InsertAt(dr, 0)
cboCategoryList.DataSource = dt
cboCategoryList.DataValueF ield = "ID";
cboCategoryList.DataTextFi eld = "Description";
cboCategoryList.DataBind() ;
dim dt as new datattable
dt=database.GetInventoryCa
Dim dr As DataRow = dt.NewRow
dr("ID") = "-1"
dr("Description") = "please select "
dt.Rows.InsertAt(dr, 0)
cboCategoryList.DataSource
cboCategoryList.DataValueF
cboCategoryList.DataTextFi
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.
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.
{
list.Insert(0, "Choose Item");
dropdownlist1.datasource = list;
dropdownlist1.dataBind();
}