Can we set a static value at the end of the dropdown?

I have two dropdown lists. Country and state. Both lists are populated from the database. In the Drop Down list for states, I want to add "Others" at the end of the list. I tried adding "Others" from the database, but when I use order by name to display the name of states alphabetically in my dropdown, even "Other" gets reordered, but I want to display it at the end of the list.

This is my code how I populate my list

private void FillStateDDl()
{
    DataSet ds = new DataSet();
    try
    {
        SqlParameter[] sql = new SqlParameter[2];
        sql[0] = new SqlParameter("@Type", 2);
        sql[1] = new SqlParameter("@cid", drpCountry.SelectedValue);
        ds = gs.DisplayData("sp_FillDropDownList", sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            drpState.DataTextField = "statename";
            drpState.DataValueField = "sid";
            drpState.DataSource = ds;
            drpState.DataBind();
            drpState.Items.Insert(0, new ListItem("--Select State--", "-1"));
        }

    }
    catch (Exception ex)
    {
        Response.Write(ex.Message.ToString());
    }
}

      

+3


source to share


4 answers


You need to make sure the parameter is AppendDataBoundItems

set to true.

Items can then be added after the data source, for example:



ddl.AppendDataBoundItems = true;
ddl.DataSource = CreateDataSource();
ddl.DataTextField="TextField";
ddl.DataValueField="ValueField";
ddl.DataBind();
ddl.Items.Add("Static value here....");

      

+2


source


Yes, you can set a static value using the datasource in the dropdown. You need to follow these steps:



  • Select the dropdown properties and find AppendDataBoundItems and set AppendDataBoundItems = true.

  • After binding the dropdown, add the following line of code ddl.Items.Add ("Others") to vb.net

  • Also order descending

+2


source


In SQL, you can do something like this

;WITH C AS(
    SELECT 1 AS RowId, .... -- Main query
    UNION ALL
    SELECT 2 AS RowId, ... -- Include others here 
)
SELECT * FROM C
ORDER BY RowId

      

+1


source


Try it,

drpState.Items.Insert(drpState.Items.Count - 1, new ListItem("--Select State--", "-1"));

      

0


source







All Articles