How to create a datatable dynamically in ASP.NET
C# Code
public partial class DynamicGrid : System.Web.UI.Page
{
static DataTable dt = new DataTable();
DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
dr = dt.NewRow();
dr["FirstName"] = TextBox1.Text;
dr["LastName"] = TextBox2.Text;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
C# Code
public partial class DynamicGrid : System.Web.UI.Page
{
static DataTable dt = new DataTable();
DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
dr = dt.NewRow();
dr["FirstName"] = TextBox1.Text;
dr["LastName"] = TextBox2.Text;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
No comments:
Post a Comment