删除网格视图中的特定行

时间:2014-04-17 10:54:00

标签: c# asp.net gridview

在下面的代码中,我有一个网格视图,其中我有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++;
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

我认为你需要再次绑定gridview数据源。

  gvInvoice.DataSource = dtCurrentTable;
  gvInvoice.DataBind()

删除数据表后放置此代码

答案 1 :(得分:0)

您正在从gridview中删除该行,但您将再次调用databind,这只是将gridview刷新到原始数据源所在的状态。

从数据源中删除它,然后数据绑定,或数据绑定并从gridview中删除它而不进行重新数据绑定。

  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();
    }

答案 2 :(得分:0)

您必须更新数据库尝试提供删除存储过程并应用此网格数据源。