Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Add multiple data GridView and Display GridView Edit Row Data in TextBox Outside GridView in ASP.NET
WhatsApp
Pintoo Yadav
Aug 28
2015
2.8
k
0
1
Add multiple data G
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 =
""
;
}
}
ASP.NET
Gridview
Textbox
Up Next
Add multiple data GridView and Display GridView Edit Row Data in TextBox Outside GridView in ASP.NET