Dynamic table in the sense to create a data table dynamically and add rows one by one programmatically. After creating Data Table and adding rows, we can bind that data table to any data grid or grid view as a data source.
Why and Where We Need It?
Suppose we are developing a Shopping Cart application or creating a wish list or adding sub category and want to add database after adding category, at that time, we need any data source to hold it temporarily. In the above circumstances, we can use data table very effectively.
Here is the Working Code
I had created a Class file for creating a Data Table and Column, but you can use it as per your requirement.
DynamicTable.cs Class File
Here, we will create a table width name and Id two fields.
public class DynamicTable
{
public DynamicTable() { }
public DataTable CreateDataSource()
{
DataTable dt = new DataTable();
DataColumn identity = new DataColumn("ID", typeof(int));
dt.Columns.Add(identity);
dt.Columns.Add("Name", typeof(string));
return dt;
}
public void AddRow(int id,string name, DataTable dt)
{
dt.Rows.Add(new object[] { id,name,pname });
}
}
Here is our aspx page:
<body>
<form id="form1" runat="server">
<div>
<table style="width:50%;">
<tr>
<td>
Add Name<td>
<td>
<asp:TextBox ID="TextBox1" runat="server"><asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Add Name"
onclick="Button1_Click" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:GridView ID="GridView1"
runat="server" AutoGenerateColumns="false"
DataKeyNames="ID" EmptyDataText ="There are no data here yet!"
onrowcommand="GridView1_RowCommand"
onrowdatabound="GridView1_RowDataBound"
onrowdeleting="GridView1_RowDeleting" >
<Columns>
<asp:TemplateField>
<HeaderTemplate>NameHeaderTemplate>
<ItemTemplate>ItemTemplate>
<asp:TemplateField>
<asp:TemplateField HeaderText="Remove">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandArgument=''
CommandName="Delete" runat="server">Removeasp:LinkButton>
<ItemTemplate>
<asp:TemplateField>
<Columns>
<asp:GridView>td>
</tr>
</table>
</div>
</form>
</body>
Here is the aspx.cs file:
When we add a new name to data table, this code will fire. Here, we are maintaining session for datacolumid
and to hold that data table line has its own comment so you can understand it easily.
protected void Button1_Click(object sender, EventArgs e)
{
DynamicTable Dt = new DynamicTable();
DataTable d = null;
if (Session["dt"] == null)
{
d = Dt.CreateDataSource();
Session["IDClm"] = 1;
}
else
{
Session["IDClm"] = Convert.ToInt16(Session["IDClm"]) + 1;
d = (DataTable)Session["dt"];
}
int ID = Convert.ToInt16(Session["IDClm"]);
string name = TextBox1.Text;
Dt.AddRow(ID, name, d);
GridView1.DataSource = d;
GridView1.DataBind();
Session["dt"] = d;
}
Whenever user wants to delete record from Data Table, then ask for confirmation "Are you sure?" Something like this. This code I implement in RowDataBound
Event of the gridView1
as follows:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
l.Attributes.Add("onclick", "javascript:return " +
"confirm('Are you sure you want to delete this Name " +
DataBinder.Eval(e.Row.DataItem, "id") + "')");
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int ID = Convert.ToInt32(e.CommandArgument);
DataTable updt = (DataTable)Session["dt"];
int i = 0;
while (i <>)
{
if (Convert.ToInt16(updt.Rows[i]["ID"]) == ID)
updt.Rows[i].Delete();
i++;
}
updt.AcceptChanges();
GridView1.DataSource = updt;
GridView1.DataBind();
Session["dt"] = updt;
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}