Add multiple data GridView and Display GridView Edit Row Data in TextBox Outside GridView in ASP.NET

  1. DataTable dt = null;  
  2. protected void Page_Load(object sender, EventArgs e) {  
  3.     if (!IsPostBack) {  
  4.         if (ViewState["Details"] == null) {  
  5.             DataTable dataTable = new DataTable();  
  6.             dataTable.Columns.Add("Name");  
  7.             dataTable.Columns.Add("Address");  
  8.             ViewState["Details"] = dataTable;  
  9.         }  
  10.     }  
  11.     //if (!this.IsPostBack)  
  12.     //{  
  13.     // DataTable dt = new DataTable();  
  14.     // dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),  
  15.     // new DataColumn("Name", typeof(string)),  
  16.     // new DataColumn("Country",typeof(string)) });  
  17.     // dt.Rows.Add(1, "John Hammond", "United States");  
  18.     // dt.Rows.Add(2, "Mudassar Khan", "India");  
  19.     // dt.Rows.Add(3, "Suzanne Mathews", "France");  
  20.     // dt.Rows.Add(4, "Robert Schidner", "Russia");  
  21.     // GridView1.DataSource = dt;  
  22.     // GridView1.DataBind();  
  23.     //}  
  24. }  
  25. protected void OnSelectedIndexChanged(object sender, EventArgs e) {  
  26.     GridViewRow row = GridView1.SelectedRow;  
  27.     // txtId.Text = row.Cells[0].Text;  
  28.     txtName.Text = row.Cells[0].Text;  
  29.     txtCountry.Text = row.Cells[1].Text;  
  30.     txtCountry.Text = (row.FindControl("lblCountry") as Label).Text;  
  31. }  
  32. protected void Button1_Click(object sender, EventArgs e) {  
  33.     // string strs = txtId.Text.Trim();  
  34.     string str = txtName.Text.Trim();  
  35.     string str1 = txtCountry.Text.Trim();  
  36.     dt = (DataTable) ViewState["Details"];  
  37.     dt.Rows.Add(str, str1);  
  38.     ViewState["Details"] = dt;  
  39.     GridView1.DataSource = dt;  
  40.     GridView1.EmptyDataText = "Name";  
  41.     GridView1.EmptyDataText = "Address";  
  42.     GridView1.DataBind();  
  43.     txtId.Text = "";  
  44.     txtName.Text = "";  
  45.     txtCountry.Text = "";  
  46. }  
  47. }