簡體   English   中英

在網格視圖中刪除特定行

[英]To delete a particular row in grid view

在下面的代碼中,我有一個網格視圖,其中有6列,其中有1個dropdown和5個textbox我的目的是刪除特定行。我嘗試過但無法刪除特定行。

protected void gvInvoice_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

    DataTable dtCurrentTable = new DataTable();
    int RowIndex = e.RowIndex;
    dtCurrentTable = (DataTable)ViewState["CurrentTable"];
    dtCurrentTable.Rows.RemoveAt(e.RowIndex);

    SetPreviousData1(dtCurrentTable);


}
private void SetPreviousData1(DataTable dtCurrentTable)
{
    int rowIndex = 0;
    if (dtCurrentTable != null)
    {
        DataTable dt = dtCurrentTable;
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Label lblProductID = (Label)gvInvoice.Rows[rowIndex].Cells[1].FindControl("lblProductID_i");
                DropDownList txtProductName = (DropDownList)gvInvoice.Rows[rowIndex].Cells[2].FindControl("EditableddlProductName");
                TextBox txtQuantity = (TextBox)gvInvoice.Rows[rowIndex].Cells[3].FindControl("txtQuantity");
                TextBox txtProductPrices = (TextBox)gvInvoice.Rows[rowIndex].Cells[4].FindControl("lblProductPrices");
                TextBox txtProfitPrice = (TextBox)gvInvoice.Rows[rowIndex].Cells[5].FindControl("txtProfitPrice");
                TextBox txtTaxCalculation = (TextBox)gvInvoice.Rows[rowIndex].Cells[6].FindControl("txtTaxCalculation");
                TextBox txtTotaPrice = (TextBox)gvInvoice.Rows[rowIndex].Cells[7].FindControl("txtTotaPrice");

                lblProductID.Text = dt.Rows[i]["Column1"].ToString();
                txtProductName.Text = dt.Rows[i]["Column2"].ToString();
                txtQuantity.Text = dt.Rows[i]["Column3"].ToString();
                txtProductPrices.Text = dt.Rows[i]["Column4"].ToString();
                txtProfitPrice.Text = dt.Rows[i]["Column5"].ToString();
                txtTaxCalculation.Text = dt.Rows[i]["Column6"].ToString();
                txtTotaPrice.Text = dt.Rows[i]["Column7"].ToString();

                rowIndex++;
            }
        }
    }
}

我認為您需要再次綁定gridview數據源。

  gvInvoice.DataSource = dtCurrentTable;
  gvInvoice.DataBind()

在刪除數據表后放置此代碼

您正在從gridview中刪除該行,但隨后又要再次調用databind,這只是將gridview刷新為原始數據源所處的狀態。

從數據源中將其刪除,然后進行數據綁定,或者從DataGrid中將其刪除,而無需重新綁定數據。

  protected void gvInvoice_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        myobj.myconnection();// connection created
        string mystr = "Delete table_name where water_id= '" + gvInvoice.DataKeys[e.RowIndex].Value + "'";// query
        sqlcmd = new SqlCommand(mystr, myobj.mycon);
        sqlcmd.ExecuteNonQuery();
        fillgrid();
    }

您必須更新您的Datebase,嘗試給出delete存儲過程並應用到此網格數據源。

暫無
暫無

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

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