单击时,Asp.net Gridview AutoGenerateEditButton“编辑”不起作用

时间:2013-10-01 12:27:14

标签: c# asp.net .net gridview aspxgridview

我正在尝试使我的gridview可编辑,但当我点击“编辑”时,我会得到一个空白页

我的aspx代码:

        <asp:Panel ID="pnl_usuarios" runat="server">
            <asp:GridView ID="grd_usuarios" runat="server" Width="100%" OnRowEditing="grd_usuarios_RowEditing"
                OnRowCancelingEdit="grd_usuarios_RowCancelingEdit"
                AutoGenerateEditButton="true" AutoGenerateColumns="false">

                <Columns>
                    <asp:BoundField DataField="Nome" HeaderText="Nome">
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"/>
                    </asp:BoundField>

                    <asp:CheckBoxField DataField="Ativo" HeaderText="Ativo">
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"/>
                    </asp:CheckBoxField>

                    <asp:CheckBoxField DataField="Administracao" HeaderText="Administração">
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"/>
                    </asp:CheckBoxField>

                    <asp:CheckBoxField DataField="VisaoGeral" HeaderText="Visão Geral">
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"/>
                    </asp:CheckBoxField>

                    <asp:CheckBoxField DataField="Estatisticas" HeaderText="Estatístcas">
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"/>
                    </asp:CheckBoxField>

                    <asp:CheckBoxField DataField="Help" HeaderText="Help">
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"/>
                    </asp:CheckBoxField>

                </Columns>

            </asp:GridView>
        </asp:Panel>

我的代码

    protected void grd_usuarios_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grd_usuarios.EditIndex = e.NewEditIndex;
        grd_usuarios.DataBind();
    }

protected void grd_usuarios_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grd_usuarios.EditIndex = -1;
        grd_usuarios.DataBind();
    }

我的问题是,如何让“编辑”按钮变为可编辑记录?

提前致谢。

PS:解决这个问题后,我会担心将数据存储在数据库中。

1 个答案:

答案 0 :(得分:0)

您需要在rowedit事件的rowcanel事件中再次绑定gridview,您的代码也会像这样

protected void grd_usuarios_RowEditing(object sender, GridViewEditEventArgs e)
{
       grd_usuarios.EditIndex = e.NewEditIndex;
       bindGridview();
}

public void  bindGridview()
{
   grd_usuarios.DataSource = yourDataTable;
   grd_usuarios.DataBind();
}