Hi,
Try this code template for creating dynamic tables on run time using datatable data...
Create a drop down list using asp control and on the selected index change event get the desired filter data from db
and assign the data table to the method to create a new table and assign it to the div tag...
.dyngridrow {
padding:10px;
color:#666;
background-color:#eeeeee;
font-size:14px;
border:0;
border:2px solid #fff;
}
.dyngridrow td {
padding:10px;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace POC
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack) { }
else
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("cardcode", typeof(string));
dt.Columns.Add("cardname", typeof(string));
dt.Rows.Add(1, "cc", "jjj");
dt.Rows.Add(2, "aa", "mmmm");
dt.Rows.Add(3, "dd", "vvv");
dt.Rows.Add(4, "ww", "mmm");
var table = GenerateGrid(dt);
divcontainer.Controls.Add(table);
}
}
private Table GenerateGrid(DataTable dt)
{
Table tblGrid = new Table() { CellPadding = 2, CellSpacing = 2 };
tblGrid.Style.Add("width", "100%");
tblGrid.Style.Add("font-family", "Segoe UI");
tblGrid.Style.Add("font-size", "14px");
TableRow tblHeaderrow = new TableRow() { CssClass = "dyngridheaderrow" };
tblHeaderrow.Cells.Add(new TableCell() { Text = " No" });
tblHeaderrow.Cells.Add(new TableCell() { Text = "Card Code" });
tblHeaderrow.Cells.Add(new TableCell() { Text = "Card Name" });
tblGrid.Rows.Add(tblHeaderrow);
foreach (DataRow row in dt.Rows)
{
TableRow trrow = new TableRow() { CssClass = "dyngridrow" };
trrow.Cells.Add(new TableCell() { Text = row["id"]+""});
trrow.Cells.Add(new TableCell() { Text = row["cardcode"] + "" });
trrow.Cells.Add(new TableCell() { Text = row["cardname"] + "" });
tblGrid.Rows.Add(trrow);
}
return tblGrid;
}
}
}