In this I will explain how we can create
a dynamic table in Asp.NET (Server side). Suppose we want to show data in
gridview from temporary dynamic table created in server side.
Design Section:
<asp:GridView ID="grdbook" runat="server">
<HeaderStyle Font-Bold="true"/>
</asp:GridView>
ASP.NET Code:
using System.Data;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bindgrid();
}
}
public void Bindgrid()
{
try
{
DataTable dt = new DataTable();
dt.Columns.Add("EmpId", typeof(Int32));
dt.Columns.Add("Emp Name", typeof(string));
dt.Columns.Add("Salary", typeof(int));
dt.Rows.Add(1, "Ajay", 12300);
dt.Rows.Add(2, "Ankush", 15000);
dt.Rows.Add(3, "Aman", 19900);
dt.Rows.Add(4, "Vijay", 29900);
dt.Rows.Add(5, "Mayank", 39900);
dt.Rows.Add(6, "Aman Sharma", 14900);
grdbook.DataSource = dt;
grdbook.DataBind();
}
catch (Exception ex)
{
}
}
VB Code:
Imports System.Data
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) HandlesMe.Load
If Not IsPostBack Then
Bindgrid()
End If
End Sub
Public Sub Bindgrid()
Try
Dim dt As New DataTable()
dt.Columns.Add("EmpId", GetType (Int32))
dt.Columns.Add("Emp Name", GetType (string))
dt.Columns.Add("Salary", GetType (int))
dt.Rows.Add(1, "Ajay", 12300)
dt.Rows.Add(2, "Ankush", 15000)
dt.Rows.Add(3, "Aman", 19900)
dt.Rows.Add(4, "Vijay", 29900)
dt.Rows.Add(5, "Mayank", 39900)
dt.Rows.Add(6, "Aman Sharma", 14900)
grdbook.DataSource = dt
grdbook.DataBind()
Catch ex As Exception
End Try
End Sub
0 comments:
Post a Comment