Saturday, June 27, 2009

Dynamic DataTable in Asp.Net

DataTable myDt;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
myDt = new DataTable();
myDt = createDataTable();
Session["myDatatable"] = myDt;

this.GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
this.GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
addDataTableData();
populateGridView();
}

private DataTable createDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", Type.GetType("System.String"));
dt.Columns.Add("Name", Type.GetType("System.String"));
return dt;
}

private void populateGridView()
{
GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
GridView1.DataBind();
}

private void addDataTableData()
{
DataTable myTable = ((DataTable)Session["myDatatable"]);
DataRow row;

row = myTable.NewRow();

row["id"] = TextBox1.Text;
row["Name"] = TextBox2.Text;

myTable.Rows.Add(row);
}

Motivational qoutes

पूरे विश्वास के साथ अपने सपनों की तरफ बढ़ें। वही ज़िन्दगी जियें जिसकी कल्पना आपने की है।