Click here to Skip to main content
11,576,692 members (57,257 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
_Amy52.9K
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 251
1 Abhinav S 170
2 DamithSL 155
3 OriginalGriff 105
4 Lucifier Rocks 105
0 OriginalGriff 715
1 Sergey Alexandrovich Kryukov 631
2 Abhinav S 528
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 365


Advertise | Privacy | Mobile
Web04 | 2.8.150603.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