Search Asp.Net Here

How to Create Data Table in Asp.Net and C# Programatically




Creating Data Table in Asp.Net and C# Programatically

Data Table of Dot Net is similar to a Database Table. Data Table can store Data in rows and each row store one reocrd.

Data Table is a class which is under System.Data Namespace of Dot net Framework

Like Database Tables we can also set DataTable's column to Auto Increment and can aslo make primary key of a Data Table’s Cloumn

In my example I am showing how to

Create Data Table
Create Column in Data Table
Make a Column Auto Increment
Make a Cloumn Primary Key
Insert a record in Data Table
Delete a record in Data Table
Update a record in Data Table


I am using Grid View Control for insert,delete and update operation on Data Table and View State is used to maintain the state of Data Table between Post Backs

Paste the following Code in your .aspx Page

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="eno" ForeColor="#333333" GridLines="None"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowCommand="GridView1_RowCommand" OnRowDeleting="GridView1_RowDeleting"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
ShowFooter="True" Caption="http://howinaspnet.blogspot.com">
<Columns>
<asp:TemplateField HeaderText="Emp NO.">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("eno") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="Label3" runat="server" Text=""></asp:Label>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("eno") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emp Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("ename") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("ename") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emp Address">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%#Eval("eadd") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("eadd") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emp Sal">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%#Eval("esal") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("esal") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="update">Update</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
 <asp:LinkButton ID="LinkButton4" runat="server" CommandName="save">Save</asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit">Edit</asp:LinkButton>
<asp:LinkButton ID="LinkButton5" runat="server" CommandName="delete">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<EmptyDataTemplate>
No Data Available<br />
</EmptyDataTemplate>
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>


Now Paste the following code in your Page_Load Event

if (Page.IsPostBack == false)
grid_bind();


Now Paste the following code Below of your Page_Load Event

//Method Creating Data Table, Column , Auto Increment Cloumn, Primary Key and Data Row


private void datatable()
{
DataTable tb = new DataTable("emp");

//Creating column in Data Table
tb.Columns.Add(new DataColumn("eno", Type.GetType("System.Int32")));
tb.Columns.Add(new DataColumn("ename", Type.GetType("System.String")));
tb.Columns.Add(new DataColumn("eadd", Type.GetType("System.String")));
tb.Columns.Add(new DataColumn("esal", Type.GetType("System.String")));

//Making Employee no (eno) column auto increment
tb.Columns["eno"].AutoIncrement = true;
tb.Columns["eno"].AutoIncrementSeed = 1;

//making Primaty key of eno column
DataColumn[] pk = new DataColumn[1];
pk[0] = tb.Columns["eno"];
tb.PrimaryKey = pk;
//Inserting Default row in Data Table
DataRow r = tb.NewRow();
r[1] = "Rahul Choudhary";
r[2] = "Chandigarh";
r[3] = "15000";
tb.Rows.Add(r);//Adding row in Data Table
//saving DataTable in Viewstate
ViewState["emp"] = tb;
}

//Method Binding Grid View

private void grid_bind()
{
if (ViewState["emp"] == null)
{
datatable();
}
DataTable tb1 = (DataTable)ViewState["emp"];
GridView1.DataSource = tb1;
GridView1.DataBind();
}

//Event to save Data in Data Table

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "save")
{
String en, ed,es;
DataTable tb2 = (DataTable)ViewState["emp"];

en = ((TextBox)(GridView1.FooterRow.FindControl("TextBox2"))).Text;
ed = ((TextBox)(GridView1.FooterRow.FindControl("TextBox4"))).Text;
es = ((TextBox)(GridView1.FooterRow.FindControl("TextBox6"))).Text;
//Addding Record in Data Table
DataRow r = tb2.NewRow();
r[1] = en;
r[2] = ed;
r[3] = es;
tb2.Rows.Add(r);
GridView1.EditIndex = -1;
grid_bind();
}
}

//Deleting Record in Data Table

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable tb3 = (DataTable)ViewState["emp"];
tb3.Rows.RemoveAt(e.RowIndex);
GridView1.EditIndex = -1;
grid_bind();

}

//Updating Record in Data Table

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
String en, ed,es;
DataTable tb4 = (DataTable)ViewState["emp"];
en = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox1"))).Text;
ed = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox3"))).Text;
es = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox5"))).Text;
tb4.Rows[e.RowIndex][1] = en;
tb4.Rows[e.RowIndex][2] = ed;
tb4.Rows[e.RowIndex][3] = es;
GridView1.EditIndex = -1;
grid_bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
grid_bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
grid_bind();
}


Download Source Code





Comments :

0 comments to “How to Create Data Table in Asp.Net and C# Programatically”

Post a Comment