using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } private void BindGrid(int rowcount) { DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new System.Data.DataColumn("FIRST NAME", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("FATHER NAME", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("LAST NAME", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("AGE", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("EMAIL", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("TELEPHONE", typeof(String))); if (ViewState["CurrentData"] != null) { for (int i = 0; i < rowcount + 1; i++) { dt = (DataTable)ViewState["CurrentData"]; if (dt.Rows.Count > 0) { dr = dt.NewRow(); dr[0] = dt.Rows[0][0].ToString(); } } dr = dt.NewRow(); dr[0] = txtname.Text; dr[1] = txtfname.Text; dr[2] = txtlname.Text; dr[3] = txtage.Text; dr[4] = txtemail.Text; dr[5] = txttele.Text; dt.Rows.Add(dr); } else { dr = dt.NewRow(); dr[0] = txtname.Text; dr[1] = txtfname.Text; dr[2] = txtlname.Text; dr[3] = txtage.Text; dr[4] = txtemail.Text; dr[5] = txttele.Text; dt.Rows.Add(dr); } // If ViewState has a data then use the value as the DataSource if (ViewState["CurrentData"] != null) { GridView1.DataSource = (DataTable)ViewState["CurrentData"]; GridView1.DataBind(); } else { // Bind GridView with the initial data assocaited in the DataTable GridView1.DataSource = dt; GridView1.DataBind(); } // Store the DataTable in ViewState to retain the values ViewState["CurrentData"] = dt; } protected void btnadd_Click(object sender, EventArgs e) { // Check if the ViewState has a data assoiciated within it. If if (ViewState["CurrentData"] != null) { DataTable dt = (DataTable)ViewState["CurrentData"]; int count = dt.Rows.Count; BindGrid(count); } else { BindGrid(1); } txtname.Text = string.Empty; txtfname.Text = string.Empty; txtlname.Text = string.Empty; txtage.Text = string.Empty; txtemail.Text = string.Empty; txttele.Text = string.Empty; txtname.Focus(); txtfname.Focus(); txtlname.Focus(); txtage.Focus(); txtemail.Focus(); txttele.Focus(); } }
void SavaData() { //Save your texboxes values to database //Call the bind gridview method to display in list or grid BindDatainGridview(); } void BindDatainGridview() { //Get list of values //Bind to gridview }
ViewState
GridView
object[] s = new object[4]; s[0] = textBox1.Text; s[1] = textBox2.Text; s[2] = textBox3.Text; s[3] = textBox4.Text; dataGridView1.Rows.Add(s);
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)