Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Item in Dropdown list in cs file

This is my CS file for dropdown:

protected void BindDropDownList()     

    {

        DataTable dt = new DataTable();
        string connString = System.Configuration.ConfigurationManager.AppSettings["EyeProject"];
        SqlConnection conn = new SqlConnection(connString);


        try
        {
            conn.Open();
            string sqlStatement = "SELECT FirstName FROM tbl_UserDetails";
            SqlCommand sqlCmd = new SqlCommand(sqlStatement, conn);
            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

            sqlDa.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                DropDownList1.DataSource =dt;


                DropDownList1.DataTextField = "FirstName"; // the items to be displayed in the list items

                DropDownList1.DataBind();
            }
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "No Data Found To display in the DropDown List";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
            conn.Close();
        }


    }



By using this one iam getting values of table Firstname values now i want to add one more item Called ALLrecords. 

How can i add it.

this is my Aspx file 

 <div class="label">
                                    Select Name:
                                    <asp:DropDownList ID="DropDownList1" runat="server">

                                    </asp:DropDownList>
                                </div>
like image 211
Venkatesh Chittepu Avatar asked Jul 31 '12 06:07

Venkatesh Chittepu


People also ask

How do I create a dropdown value in typescript?

First, create the template variables for dropdown using #teams . Use the event binding to listen for the change event and link with the onSelect method. The onSelect method receives the reference variable and takes the value of the dropdown element. Next, create the onSelected(value) method in the TypeScript file.

What is dropdown list in C#?

The DropDownList control is a web server element that creates the drop-down menu and lets users select a single item from various options. You can add any number of items to the DropDownList.

What is a list of dropdown options or items?

Dropdown lists display a single default or selected value enclosed in a container box. On click of the down-arrow button, a list of options appears from which users can select only one.


3 Answers

Try this

 DropDownList1.Items.Add(new ListItem("All Record"));

and if you want to add item with value then

 DropDownList1.Items.Add(new ListItem("All Record","0"));

 //or if you want to add at particular index then

 DropDownList1.Items.Insert(0,new ListItem("All Record"));// 0 is index of item

hope this helps.

like image 50
Manish Parakhiya Avatar answered Oct 20 '22 02:10

Manish Parakhiya


insert an item at specified index

DropDownListID.Items.Insert(0, new ListItem("Default text", "Default value")
like image 36
fengd Avatar answered Oct 20 '22 01:10

fengd


DropDownList1.Items.Insert(0,new ListItem("AllRecords","itsValue_on_dropdownlist")); // use 0 to show "ALLRecords" text on top in dropdownlist

i would suggest that you should bind values to dropdownlist also. like this -

DropDownList1.DataValueField = "FirstName";
like image 39
Vikash Singh Avatar answered Oct 20 '22 00:10

Vikash Singh