想要在单击命令drop时从gridview中删除行 [英] Want do delete row from gridview when command drop is clicked

查看:90
本文介绍了想要在单击命令drop时从gridview中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您点击gridview中的drop(命令字段)时,某些行会被删除,而其他行则不会删除



相同的问题将应用于注释行当我也尝试它们时



我尝试过:



HTML



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: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>





C#





The 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();
                
            }
        }

推荐答案

您是否在调试模式下检查了e的值。 RowIndex是要确保它要删除的行吗?



另外,我假设你在初始化时用另一种方法将DataTable绑定到GridView,并且所以不需要在delete方法中再次完成。
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.


这篇关于想要在单击命令drop时从gridview中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆