project ở đây mô tả ngắn gọn như sau: mình muốn save dữ liệu từ textbox xuốg datagridview nhưng hok dùng database mà dúng datatable...mình đã save và update nhưng giờ delete một dòng thì mình không biết phải làm sao...các bạn giúp mình với..
Mã:
using System;using System.Data;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls; namespace WebApplication1{ public partial class WebForm1 : System.Web.UI.Page { DataTable dt = new DataTable(); private void BindGrid(int rowcount) { //DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new System.Data.DataColumn("ID", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("Name", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("Class", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("Score 1", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("Score 2", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("Ava", 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(); Int32 total = (Convert.ToInt32(txtd1.Text) + Convert.ToInt32(txtd2.Text)) / 2; dr[0] = txtid.Text; dr[1] = txtname.Text; dr[2] = DropDownList1.SelectedItem; dr[3] = txtd1.Text; dr[4] = txtd2.Text; dr[5] = total; dt.Rows.Add(dr); } else { dr = dt.NewRow(); Int32 total = (Convert.ToInt32(txtd1.Text) + Convert.ToInt32(txtd2.Text)) / 2; dr[0] = txtid.Text; dr[1] = txtname.Text; dr[2] = DropDownList1.SelectedItem; dr[3] = txtd1.Text; dr[4] = txtd2.Text; dr[5] = total; 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 Button1_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); } txtid.Text = ""; txtname.Text = ""; txtd1.Text = ""; txtd2.Text = ""; txttb.Text = ""; } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { txtid.Text = GridView1.SelectedRow.Cells[2].Text; txtname.Text = GridView1.SelectedRow.Cells[3].Text; txtd1.Text= GridView1.SelectedRow.Cells[5].Text; txtd2.Text = GridView1.SelectedRow.Cells[6].Text; txttb.Text = GridView1.SelectedRow.Cells[7].Text; } protected void Button2_Click(object sender, EventArgs e) { GridView1.SelectedRow.Cells[2].Text = txtid.Text; GridView1.SelectedRow.Cells[3].Text = txtname.Text; GridView1.SelectedRow.Cells[4].Text = DropDownList1.SelectedItem.ToString(); GridView1.SelectedRow.Cells[5].Text = txtd1.Text; GridView1.SelectedRow.Cells[6].Text = txtd2.Text; GridView1.SelectedRow.Cells[7].Text = txttb.Text; } protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e) { } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { //if (e.CommandName == "Delete") //{ // GridView1.DeleteRow(Convert.ToInt32(e.CommandArgument)); //dt.Rows.RemoveAt(Convert.ToInt32(e.CommandArgument)); //} //GridView1.DataSource = dt; //GridView1.DataBind(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { dt.Rows.Clear(); dt.AcceptChanges(); GridView1.DataSource = dt; GridView1.DataBind(); } protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e) { } protected void Button3_Click(object sender, EventArgs e) { //dt.Rows[1].Delete(); //GridView1.DataSource = dt; //GridView1.DataBind(); int catid = int.Parse(GridView1.DataKeys[0].Value.ToString()); dt.Rows[catid].Delete(); GridView1.DataSource = dt; GridView1.DataBind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { } }}
View more random threads:
Thông qua phân tích dữ liệu Google từ 86 quốc gia, mới đây, một công ty tại Anh đã công bố bảng xếp hạng kích tấc "cậu nhỏ" của các nước trên thế giới. Kết quả, hầu hết các nước xếp ở nhóm đầu của...
"Chim" của chàng trai Việt thuộc...