TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Add multiple data GridView and Display GridView Edit Row Data in TextBox Outside GridView in ASP.NET
Pintoo Yadav
Aug 28
2015
Code
2.7
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
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