簡體   English   中英

如何從gridview刪除特定的行。使用gridview的內置刪除命令按鈕

[英]How to delete the specific row from gridview.Using builtin delete command button of gridview

嗨,我正在將文本框的數據添加到本地的gridview中,而不是數據庫中。 就像推銷員在推銷產品的銷售頁面。 現在我想從該網格視圖中刪除一行,但是刪除時我總是異常。 請幫助我。 這是我的代碼,我在其中獲取數據並將其存儲在gridview中。 謝謝。

DataTable dt1 = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            gridVIEWData();
            GridView1.DataSource = dt1;
            GridView1.DataBind();
        }
private void gridVIEWData()
    {

        dt1.Columns.Add("pName", typeof(string));
        dt1.Columns.Add("pCategory", typeof(string));
        dt1.Columns.Add("price", typeof(string));
        dt1.Columns.Add("pQuantity", typeof(string));
        dt1.Columns.Add("totalPrice", typeof(string));
        Session["dtInSession"] = dt1;   //Saving Datatable To Session
    }
protected void Button3_Click(object sender, EventArgs e)
    {
        if (Session["dtInSession"] != null)
            dt1 = (DataTable)Session["dtInSession"]; //Getting datatable from session 


            Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
            DataRow dr = dt1.NewRow();
            dr["pName"] = DropDownList2.SelectedItem;
            dr["pCategory"] = DropDownList1.SelectedItem;
            dr["price"] = txt_price.Text;
            dr["pQuantity"] = txt_quantity.Text;
            dr["totalPrice"] = total;
            dt1.Rows.Add(dr);


            Session["dtInSession"] = dt1;     //Saving Datatable To Session 

            GridView1.DataSource = dt1;
            GridView1.DataBind();

        }
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

       // string value = GridView1.DataKeys[e.RowIndex].Values["price"].ToString();
        //GridView1.DeleteRow(Convert.ToInt32(value));
        //GridView1.DeleteRow(GridView1.SelectedIndex);

           // dt1.Rows.RemoveAt(GridView1.SelectedIndex);
        Int32 index = GridView1.SelectedIndex;
        GridView1.DeleteRow(index);
    }

只是為了與您的代碼保持一致,

方法1:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
        if (Session["dtInSession"] != null)  
           dt1 = (DataTable)Session["dtInSession"];

       Int32 index = GridView1.SelectedIndex;
       dt1 = dt1.Rows[index].Delete();

       dt1.AcceptChanges();

       Session["dtInSession"] = dt1;  //Saving Datatable To Session   
       GridView1.DataSource = dt1;                         
       GridView1.DataBind();   
}

方法2:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="pName" onselectedindexchanged="GridView1_SelectedIndexChanged">

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
  string _pName = Convert.ToString(GridView1.SelectedDataKey.Value); 

  // Once you get the pName you can query from DataTable and Delete the row which the pName = _pName
  // BindGrid and Store DataTable in session.
}

這可能對您有幫助。

 protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (ViewState["CurrentTable"] != null)
                dt = (DataTable)ViewState["CurrentTable"];            
            if (dt.Rows.Count > 0)
            {
                int Index = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["Pk"].ToString());
                dt.Rows.Cast<DataRow>().Where(o => o.Field<Int32>("Pk").Equals(Index)).FirstOrDefault().Delete();
               //  dt.Rows.RemoveAt(Index-1);
                gvDetails.DataSource = dt;
                gvDetails.DataBind();
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM