Thiyagu
Thiyagu

Reputation: 3

To delete a particular row in grid view

In the below code, I have grid view in which I have 6 columns in which there are 1 dropdown and 5 textbox.My aim is to delete a particular row .I tried but I can't able to delete the particular row.

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++;
            }
        }
    }
}

Upvotes: 0

Views: 485

Answers (3)

Shaz
Shaz

Reputation: 11

You have to update your Datebase try giving delete stored procedure and apply on this grid datasource.

Upvotes: 0

Nayeem Mansoori
Nayeem Mansoori

Reputation: 831

You are deleting the row from the gridview but you are then going and calling databind again which is just refreshing the gridview to the same state that the original datasource is in.

Either remove it from the datasource and then databind, or databind and remove it from the gridview without redatabinding.

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

Upvotes: 0

KarthikManoharan
KarthikManoharan

Reputation: 805

I think you need to bind the gridview datasource again.

  gvInvoice.DataSource = dtCurrentTable;
  gvInvoice.DataBind()

place this code after deleting for datatable

Upvotes: 1

Related Questions