DataTable dt = null;
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
if (ViewState["Details"] == null) {
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Name");
dataTable.Columns.Add("Address");
ViewState["Details"] = dataTable;
}
}
//if (!this.IsPostBack)
//{
// DataTable dt = new DataTable();
// dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
// new DataColumn("Name", typeof(string)),
// new DataColumn("Country",typeof(string)) });
// dt.Rows.Add(1, "John Hammond", "United States");
// dt.Rows.Add(2, "Mudassar Khan", "India");
// dt.Rows.Add(3, "Suzanne Mathews", "France");
// dt.Rows.Add(4, "Robert Schidner", "Russia");
// GridView1.DataSource = dt;
// GridView1.DataBind();
//}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e) {
GridViewRow row = GridView1.SelectedRow;
// txtId.Text = row.Cells[0].Text;
txtName.Text = row.Cells[0].Text;
txtCountry.Text = row.Cells[1].Text;
txtCountry.Text = (row.FindControl("lblCountry") as Label).Text;
}
protected void Button1_Click(object sender, EventArgs e) {
// string strs = txtId.Text.Trim();
string str = txtName.Text.Trim();
string str1 = txtCountry.Text.Trim();
dt = (DataTable) ViewState["Details"];
dt.Rows.Add(str, str1);
ViewState["Details"] = dt;
GridView1.DataSource = dt;
GridView1.EmptyDataText = "Name";
GridView1.EmptyDataText = "Address";
GridView1.DataBind();
txtId.Text = "";
txtName.Text = "";
txtCountry.Text = "";
}
}This code snippet for Add multiple data GridView and Display GridView Edit Row data in TextBox outside GridView in ASP.Net.
