Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 6 của 6
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Xóa một dòng trong datagridview, dùng source là datatable bằng cách nào?

    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)        {                    }     }}

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    help me.........

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Thử cái này:
    Mã:
    dTable.Rows[1].Delete();
    dTable.AcceptChanges();

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    mình có thử rồi bạn, nó báo là hok có giá trị tại vị trí mình muốn xóa và báo lỗi...mặc dù mình đã insert giá trị vào rồi...hihix..thank bạn nhiều...

  5. #5
    Không biết giúp bạn được ko, nhưng mình đưa ra ý tưởng này: bạn bắt giá trị cần xóa, rồi foreach lại datatable insert vào lại điều kiện != cái value mình muốn xóa; như thế là mình có datatable mới mà ko còn value cần xóa nữa. Chúc bạn thành công!.

  6. #6
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    quan trọng là mình không biết làm sao bắt được giá trị cần xóa trên datatable thông qua datagridview. có bạn chỉ mình dùng datakeyvalue, nhưng hok biết set như thế nào????help me!!hồi giờ mình chưa làm ASP bao giờ nên hơi khó.

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •