Click here to Skip to main content
11,714,345 members (82,002 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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:-

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 4-Feb-13 19:41pm
danil33399
Edited 4-Feb-13 20:05pm
_Amy53.3K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Bad practice always.
You can find the rowindex always in your GridView RowCommand event.
Try this:
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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 540
1 Maciej Los 259
2 Richard MacCutchan 230
3 OriginalGriff 140
4 F-ES Sitecore 140
0 Sergey Alexandrovich Kryukov 545
1 CHill60 375
2 Maciej Los 324
3 Richard MacCutchan 310
4 OriginalGriff 260


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100