Click here to Skip to main content
12,830,941 members (41,028 online)
Rate this:
Please Sign up or sign in to vote.
See more: ASP.NET
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);
        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;
    if (demandSlabList.Rows.Count > 0)
        SlabsGrid.DataSource = demandSlabList;
Posted 4-Feb-13 20:41pm
Updated 4-Feb-13 21:05pm

1 solution

Rate this: bad
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.


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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170326.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2017
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