- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
In this article I will explain how to bind/fill/populate dropdownlist from a datatable in Asp.Net using C# and VB.Net.
You need to add html dropdown component in aspx page to bind/fill/populate the dropdown.
HTML
<asp:DropDownList runat="server" ID="ddlCountry"></asp:DropDownList>
You need to add a namespace in code behind file.
C#
using System.Data;
VB.Net
Imports System.Data
Now, need to initialize datatable before page load event or you can initialize datatable as per your requirement.
C#
DataTable dt = new DataTable();
VB.Net
Dim dt As DataTable = New DataTable()
Now, I will add data to the datatable on page load event or you can add data to the datatable at any event as per your requirement.
C#
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { dt.Clear(); dt.Columns.Add("Id"); dt.Columns.Add("Country"); DataRow dr = dt.NewRow(); dr["Id"] = 1; dr["Country"] = "India"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Id"] = 2; dr["Country"] = "Australia"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Id"] = 3; dr["Country"] = "Srilanka"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Id"] = 4; dr["Country"] = "Bangladesh"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Id"] = 5; dr["Country"] = "New Zealand"; dt.Rows.Add(dr); BindDropdownList(); } }
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not Me.IsPostBack Then dt.Clear() dt.Columns.Add("Id") dt.Columns.Add("Country") Dim dr As DataRow = dt.NewRow() dr("Id") = 1 dr("Country") = "India" dt.Rows.Add(dr) dr = dt.NewRow() dr("Id") = 2 dr("Country") = "Australia" dt.Rows.Add(dr) dr = dt.NewRow() dr("Id") = 3 dr("Country") = "Srilanka" dt.Rows.Add(dr) dr = dt.NewRow() dr("Id") = 4 dr("Country") = "Bangladesh" dt.Rows.Add(dr) dr = dt.NewRow() dr("Id") = 5 dr("Country") = "New Zealand" dt.Rows.Add(dr) BindDropdownList() End If End Sub
Now, I will create the function BindDropdownList().
C#
public void BindDropdownList() { if (dt.Rows.Count > 0) { ddlCountry.DataSource = dt; ddlCountry.DataTextField = "Country"; ddlCountry.DataValueField = "Id"; ddlCountry.DataBind(); } ddlCountry.Items.Insert(0, new ListItem("--Select Country--", "0")); }
VB.Net
Public Sub BindDropdownList() If dt.Rows.Count > 0 Then ddlCountry.DataSource = dt ddlCountry.DataTextField = "Country" ddlCountry.DataValueField = "Id" ddlCountry.DataBind() End If ddlCountry.Items.Insert(0, New ListItem("--Select Country--", "0")) End Sub
Full Code:
HTML
<asp:DropDownList runat="server" ID="ddlCountry"></asp:DropDownList>
C#
using System; using System.Data; namespace Demo { public partial class Default : System.Web.UI.Page { private DataTable dt = new DataTable(); protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { dt.Clear(); dt.Columns.Add("Id"); dt.Columns.Add("Country"); var dr = dt.NewRow(); dr["Id"] = 1; dr["Country"] = "India"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Id"] = 2; dr["Country"] = "Australia"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Id"] = 3; dr["Country"] = "Srilanka"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Id"] = 4; dr["Country"] = "Bangladesh"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Id"] = 5; dr["Country"] = "New Zealand"; dt.Rows.Add(dr); BindDropdownList(); } } public void BindDropdownList() { if (dt.Rows.Count > 0) { ddlCountry.DataSource = dt; ddlCountry.DataTextField = "Country"; ddlCountry.DataValueField = "Id"; ddlCountry.DataBind(); } ddlCountry.Items.Insert(0, new ListItem("--Select Country--", "0")); } } }
VB.Net
Imports System Imports System.Data Imports System.Web.UI Imports System.Web.UI.WebControls Namespace Demo Public Partial Class [Default] Inherits Web.UI.Page Private dt As DataTable = New DataTable() Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not Me.IsPostBack Then dt.Clear() dt.Columns.Add("Id") dt.Columns.Add("Country") Dim dr As DataRow = dt.NewRow() dr("Id") = 1 dr("Country") = "India" dt.Rows.Add(dr) dr = dt.NewRow() dr("Id") = 2 dr("Country") = "Australia" dt.Rows.Add(dr) dr = dt.NewRow() dr("Id") = 3 dr("Country") = "Srilanka" dt.Rows.Add(dr) dr = dt.NewRow() dr("Id") = 4 dr("Country") = "Bangladesh" dt.Rows.Add(dr) dr = dt.NewRow() dr("Id") = 5 dr("Country") = "New Zealand" dt.Rows.Add(dr) BindDropdownList() End If End Sub Public Sub BindDropdownList() If dt.Rows.Count > 0 Then ddlCountry.DataSource = dt ddlCountry.DataTextField = "Country" ddlCountry.DataValueField = "Id" ddlCountry.DataBind() End If ddlCountry.Items.Insert(0, New ListItem("--Select Country--", "0")) End Sub End Class End Namespace
Demo Screenshot:
Please mail me at hemanta@webcodespider.net if you want any code solution from me.
Thank You 😊
Show Javascript Confirm Alert Box in ASP.Net from Server Side using C# and VB.Net
- Get link
- X
- Other Apps
Comments
Post a Comment