Create data Table Dynamically and bind to Drop down List in asp.net
Create data Table Dynamically and bind to Drop down List in asp.net
In this post we will
explain how to create DataTable and bind data to columns in DataTable dynamically using asp.net Programming languge.
Here we see how to Runtime
create Data Table using C# and then bind
it to DropDown List in ASP.Net Programming.
First a dynamic Data Table object is created and then its schema (Table rows and Columns) is defined by coding . first define columns then rows (records) are added to the dynamically created Data Table.
If the dynamic DataTable is fill by records, it is bound to an DropDown List control in asp.net .
First a dynamic Data Table object is created and then its schema (Table rows and Columns) is defined by coding . first define columns then rows (records) are added to the dynamically created Data Table.
If the dynamic DataTable is fill by records, it is bound to an DropDown List control in asp.net .
To design a table
visually in Design view by C# code , use an DataTable control. If you
also need to code to
create row and column and add its in to created table
|
Rows in a Table Web
server control are objects of type data
Row. Similarly, the Table Row object
has a Cells property
that supports a collection of objects of type data
column. You can add cells to a row code this collection(created table).
<html
<head runat="server">
<title>Dynamically
data Table with dropdown List in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlEMP"
runat="server" Width="128px" AutoPostBack="True">
</asp:DropDownList>
</div>
</form>
</body>
</html>
Code for coding page (.aspx.cs page) in Asp.net programming:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
public partial class DynamicTablewithDropdown : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGridview ();
}
}
/// Dynamically create & bind data to datatable and bind
datatable to gridview
protected void BindGrid ()
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(Int32));
dt.Columns.Add("Name", typeof (string));
DataRow dtrow = dt.NewRow(); // Create New Row ……………………………………….
dtrow["UserId"] = 1; //Bind Data to Columns
dtrow["UserName"] = "FIG";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); // Create New Row ……………………………………….
dtrow["UserId"] = 2; //Bind Data to Columns
dtrow["UserName"] = "CDE";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); // Create New Row ……………………………………….
dtrow["UserId"] = 3; //Bind Data to Columns
dtrow["UserName"] = "ABC";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); // Create New Row ……………………………………….
dtrow["UserId"] = 4; //Bind Data to Columns
dtrow["UserName"] = "Ram";
dt.Rows.Add(dtrow);
ddlEMP.DataSource
= dt
ddlEMP.DataTextField
= "NAME";
ddlEMP.DataValueField
= "Id";
ddlEMP.Items.Insert(0, new ListItem("Select", "0"));
ddlEMP.DataBind()
}
}
VB.NET Code
Imports System
Imports System.Data
Partial Class Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridviewData()
End If
End Sub
// Dynamically create & bind data to datatable and bind
datatable to gridview
Protected Sub BindGridviewData()
Dim dt As New DataTable()
dt.Columns.Add("UserId", GetType(Int32))
dt.Columns.Add("UserName", GetType(String))
Dim dtrow As DataRow = dt.NewRow()
// Create New Row ……………………………………….
dtrow("UserId") = 1
dtrow("UserName") = "FGH"
dt.Rows.Add(dtrow)
dtrow = dt.NewRow()
// Create New Row ……………………………………….
dtrow("UserId") = 2
dtrow("UserName") = "CDE"
dt.Rows.Add(dtrow)
dtrow = dt.NewRow()
// Create New Row ……………………………………….
dtrow("UserId") = 3
dtrow("UserName") = "ABC"
dt.Rows.Add(dtrow)
ddlEMP.DataSource
= dt
ddlEMP.DataTextField
= "NAME";
ddlEMP.DataValueField
= "Id";
ddlEMP.Items.Insert(0, new ListItem("Select", "0"));
ddlEMP.DataBind()
End Sub
End Class
Drop Down Related Old Posts of in Asp.net :
- Bind Dropdown by Dynamically
- Fill Dropdown By database.
- Adding or binding tooltip in each item of dropdownlist
- Check box in asp Dropdown List.
- DropDown List in asp.net Programming,
- Add ListItem in DropDownList asp.net c# Example
- Drop Down List control of asp.net
- Select option to the user from many items.
- Display tooltip from database for dropdownlist items in asp.net c# example
- Show tooltip dropdownlist items by mouse over event using C# example
- Example of Adding ToolTip for each Dropdown List Item in C#.
- Cropping image using jQuery in asp.net
- jquery disable or Enable submit button after Validation
- Enable Disable Submit Button using jQuery
- Dynamically creating aspx page,
- Get the current page url by C#
- jQuery modal dialog with postbacks
- Drag and Drop Sortable Lists using jQueryUI
- jquery tooltip with css in asp.net web page
- Ajax ColorPickerExtender in ASP.NET Example
- What is the Ajax colorpicker,How to use Ajax colorpicker
- JQuery UI Datepicker (Calendar) with asp.net textbox
- Using jQuery change Background color of DIV
- JQuery Change Div Background Color Randomly
- JQuery Change Div Background Color Randomly
- C# Variables,How to declare in C#:
- C# language Program Structure
- What keyword,How many keywords in C#