Introduction:
In this Article I will explain how to create datatable dynamically and bind that datatable to gridview in asp.net using c# and vb.net.
In this Article I will explain how to create datatable dynamically and bind that datatable to gridview in asp.net using c# and vb.net.
Implementation:
To implement this requirement
we will create one new datatable after that add rows and columns and assign
data to columns.
Design
Section:
Now create new website open Default.aspx page
and write the following code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Dynamically create datatable and bind data to it in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset style="width:300px"><legend><strong>Dynamically created Datatable in ASP.NET</strong></legend>
<asp:GridView ID="gvDetails" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle
BackColor="White"
ForeColor="#284775"
/>
<EditRowStyle
BackColor="#999999"
/>
<FooterStyle
BackColor="#5D7B9D"
Font-Bold="True"
ForeColor="White"
/>
<HeaderStyle
BackColor="#5D7B9D"
Font-Bold="True"
ForeColor="White"
/>
<PagerStyle
BackColor="#284775"
ForeColor="White"
HorizontalAlign="Center"
/>
<RowStyle
BackColor="#F7F6F3"
ForeColor="#333333"
/>
<SelectedRowStyle
BackColor="#E2DED6"
Font-Bold="True"
ForeColor="#333333"
/>
<SortedAscendingCellStyle
BackColor="#E9E7E2"
/>
<SortedAscendingHeaderStyle
BackColor="#506C8C"
/>
<SortedDescendingCellStyle
BackColor="#FFFDF8"
/>
<SortedDescendingHeaderStyle
BackColor="#6F8DAE"
/>
</asp:GridView></fieldset>
</div>
</form>
</body>
</html>
After
that open code behind page and add the following namespace references
using System;
using System.Data;
After add namespaces write the following code
in code behind
protected void Page_Load(object sender, EventArgs
e)
{
if(!IsPostBack)
{
BindGridviewData();
}
}
/// <summary>
/// Dynamically create & bind data to datatable and bind
datatable to gridview
/// </summary>
protected void BindGridviewData()
{
DataTable dt = new DataTable();
dt.Columns.Add("EmpId", typeof(Int32));
dt.Columns.Add("Name", typeof
(string));
dt.Columns.Add("Qualification", typeof (string));
dt.Columns.Add("Address",typeof(string));
DataRow dtrow = dt.NewRow(); // Create New Row
dtrow["EmpId"]
= 1; //Bind
Data to Columns
dtrow["Name"]
= "Anil";
dtrow["Qualification"]
= "B.Tech";
dtrow["Address"]
= "Chennai";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); //
Create New Row
dtrow["EmpId"]
= 2; //Bind
Data to Columns
dtrow["Name"]
= "Ankit";
dtrow["Qualification"]
= "M.Sc.";
dtrow["Address"]
= "Dharmshala";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); // Create
New Row
dtrow["EmpId"]
= 3; //Bind
Data to Columns
dtrow["Name"]
= "Anish";
dtrow["Qualification"]
= "MCA";
dtrow["Address"]
= "Chandigarh";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); //
Create New Row
dtrow["EmpId"]
= 4; //Bind Data to
Columns
dtrow["Name"]
= "Anushka";
dtrow["Qualification"]
= "B.Tech";
dtrow["Address"]
= "Delhi";
dt.Rows.Add(dtrow);
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
VB.NET
Code:
Use
following code :
Partial Public Class
griddatatable
Inherits System.Web.UI.Page
Protected Sub
Page_Load(sender As Object,
e As EventArgs)
If Not IsPostBack Then
BindGridviewData()
End If
End Sub
''' <summary>
''' Dynamically create & bind data to
datatable and bind datatable to gridview
''' </summary>
Protected Sub
BindGridviewData()
Dim dt As New DataTable()
dt.Columns.Add("EmpId",
GetType(Int32))
dt.Columns.Add("Name",
GetType(String))
dt.Columns.Add("Qualification",
GetType(String))
dt.Columns.Add("Address",
GetType(String))
Dim
dtrow As DataRow
= dt.NewRow()
' Create New
Row
dtrow("EmpId")
= 1
'Bind Data to
Columns
dtrow("Name")
= "Anil"
dtrow("Qualification")
= "B.Tech"
dtrow("Address")
= "Chennai"
dt.Rows.Add(dtrow)
dtrow = dt.NewRow()
' Create New
Row
dtrow("EmpId")
= 2
'Bind Data to
Columns
dtrow("Name")
= "Ankit"
dtrow("Qualification")
= "M.Sc."
dtrow("Address")
= "Dharmshala"
dt.Rows.Add(dtrow)
dtrow = dt.NewRow()
' Create New
Row
dtrow("EmpId")
= 3
'Bind Data to
Columns
dtrow("Name")
= "Anish"
dtrow("Qualification")
= "MCA"
dtrow("Address") = "Chandigarh"
dt.Rows.Add(dtrow)
dtrow = dt.NewRow()
' Create New
Row
dtrow("EmpId")
= 4
'Bind Data to
Columns
dtrow("Name")
= "Anushka"
dtrow("Qualification")
= "B.Tech"
dtrow("Address")
= "Delhi"
dt.Rows.Add(dtrow)
gvDetails.DataSource = dt
gvDetails.DataBind()
End Sub
End Class
0 comments:
Post a Comment