Gali
Gali

Reputation: 14963

how to get empty row in DropDownList

i have this citys in my database:

city1
city2
city3
city4

i what to see this in my DropDownList:

[empty]
city1
city2
city3
city4

this is my bind code:

SQL = "select distinct City from MyTbl order by City";
dsClass = new DataSet();
adp = new SqlDataAdapter(SQL, Conn);
adp.Fill(dsClass, "MyTbl");
adp.Dispose();
DropDownList3.DataSource = dsClass.Tables[0];
DropDownList3.DataTextField = dsClass.Tables[0].Columns[0].ColumnName.ToString();
DropDownList3.DataValueField = dsClass.Tables[0].Columns[0].ColumnName.ToString();
DropDownList3.DataBind();

how to do it (asp.net C#) ?

thanks in advance

Upvotes: 2

Views: 3396

Answers (3)

Vijay Sirigiri
Vijay Sirigiri

Reputation: 4711

 <asp:DropDownList ID='DropDownList1' runat='server' DataSourceID='ObjectDataSource1'
        DataTextField='ProductName' DataValueField='ProductID' AppendDataBoundItems='true'
        >
            <asp:ListItem Text="" Enabled="true" Selected="True" Value='-1'>
            </asp:ListItem>
 </asp:DropDownList>

by setting AppendDataBoundItems = true in the DropDownList markup, DropDownList would append the items from datasource after the asp:ListItem (specified in the markup).

This could be achieved from code-behind but this is lot simpler.

Upvotes: 0

abatishchev
abatishchev

Reputation: 100288

DropDownList1.Items.Insert(0, String.Empty);

which is the same as

DropDownList1.Items.Insert(0, new ListItem(String.Empty, String.Empty));

or

DropDownList1.Items.Insert(0, new ListItem(String.Empty));

or

DropDownList1.Items.Insert(0, new ListItem());

Upvotes: 2

Pankaj Agarwal
Pankaj Agarwal

Reputation: 11309

After Binding DropDownList through below mentioned code you can add new item in DropDownList

DropDownList3.Items.Insert(0, new ListItem(String.Empty, String.Empty));

You can replace String.Empty to your desire value.

Upvotes: 3

Related Questions