Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Some of the rows delete while others don't when you click on the drop (commandfield) in the gridview

The same issue is applied to the commented lines when I try them too

What I have tried:

The HTML

ASP.NET
<asp:GridView ID="Grid" runat="server" AutoGenerateColumns="False" Height="65px" OnSelectedIndexChanged="Grid_SelectedIndexChanged" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None" OnRowDeleting="Grid_RowDeleting">
            <Columns>
                <asp:BoundField HeaderText="id" DataField="id" />
                <asp:BoundField HeaderText="firstname" DataField="firstname" />
                <asp:BoundField HeaderText="lastname" DataField="lastname" />
                <asp:BoundField HeaderText="age" DataField="age" />
                <asp:CommandField DeleteText="Drop" ShowDeleteButton="True" />
            </Columns>

            <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
            <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
            <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
            <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#594B9C" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#33276A" />

        </asp:GridView>


The C#

C#
protected void Grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DataTable dtDatas = (DataTable)ViewState["dtDatas"];
            if (dtDatas.Rows.Count > 0)
            {
                //Grid.DeleteRow(Grid.SelectedIndex);
                //dtDatas.Rows.RemoveAt(0);
                dtDatas.Rows[e.RowIndex].Delete();
                Grid.DataSource = dtDatas;
                Grid.DataBind();
                
            }
        }
Posted
Updated 13-Mar-18 13:15pm
v2
Comments
Sarita Mall 22-Feb-18 0:14am    
above code is working fine.except <SortedAscendingCellStyle BackColor="#F1F1F1" />.

Please elaborate what exact error you are facing?
Laxmidhar tatwa technologies 22-Feb-18 23:30pm    
try
Grid.DataSource = null;
Grid.DataBind();
Grid.DataSource = dtDatas;
Grid.DataBind();

1 solution

Have you checked in debug mode what the value of e.RowIndex is to make sure its the row you want to delete?

Also, I assume you're binding the DataTable to the GridView in another method when its initialized, and so it doesn't need to be done again in the delete method.
 
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