Monday, 22 February 2016

How to bind DropDownList using dummy DataTable in ASP.Net with C#

In the article, I will explain you how to bind DropDownList in using dummy DataTable or DataSet. I have populated DropDownList using dummy data from code-behind C# code.

Related Articles

In Designer File

In designer file, create a DropDownList control names as ddlEmployee.

<asp:DropDownList ID="ddlEmployee" runat="server"></asp:DropDownList>

Include the below Namespace.

using System.Data;

Complete C# Code

Here, we are creating a Datatable dt and adding two columns named as Id(Int) and name(string). Added rows to DataTable and assiging to text field and value field of dropdownlist.

protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)

        public void BindDropDown()
            //Create a DataTable
            DataTable dt = new DataTable(); 
            //add Columns to DataTable
            dt.Columns.Add("Id", typeof(Int32)); 
            dt.Columns.Add("Name", typeof(string)); 
            //Add rows to DataTable
            dt.Rows.Add(1, "Rahul");
            dt.Rows.Add(2, "Vijay");
            dt.Rows.Add(3, "Yogendra");
            dt.Rows.Add(4, "Yogesh");
            ddlEmployee.DataSource = dt;
            //Assigining Text Field
            ddlEmployee.DataTextField = "Name"; 
            //Assigining Value Field
            ddlEmployee.DataValueField = "Id";
             //insert a new item at row position 0
            ddlEmployee.Items.Insert(0, new ListItem("--select--"));



Output will be shown as below.

How to bind DropDownList in using dummy DataTable


Post a Comment

Email Subscription

Subscribe to our newsletter to get the latest articles directly into your inbox

Blog Archive