Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Iam trying to delete some rows from gridview without affecting the database.So many examples I saw in the code project itself.But the problem with the below code is only the row in the zeroth position can delete.But I want to delete the row in the click position itself.Can anyone suggest any idea?

Below is my code:-

C#
protected void DeleteDemandSlabOnclick(object sender, EventArgs e)
{
    DataTable demandSlabList;
    if (txtbxTariff.Text.Contains("HT") == false)
        InitializeFixedChargeTable(out demandSlabList);
    else
        InitializeDemandChargeTable(out demandSlabList);
    foreach (GridViewRow Row in SlabsGrid.Rows)
    {
        DataRow row = demandSlabList.NewRow();
        row[0] = Row.Cells[0].Text;
        row[1] = Row.Cells[1].Text;
        row[2] = Row.Cells[2].Text;
        demandSlabList.Rows.Add(row);
    }
    if (demandSlabList.Rows.Count > 0)
    {
        demandSlabList.Rows.RemoveAt(0);
        SlabsGrid.DataSource = demandSlabList;
        SlabsGrid.DataBind();
    }
}
Posted
Updated 4-Feb-13 20:05pm
v2

1 solution

Bad practice always.
You can find the rowindex always in your GridView RowCommand event.
Try this:
C#
GridViewRow gvr = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);

int RowIndex = gvr.RowIndex; 


Now you can delete the row using RemoveAt function. You need to pass Index as this RowIndex.


--Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900