如何在不丢失 rowdatabound 更改的情况下编辑 gridview?

How to edit gridview without loosing rowdatabound changes?

我在 vb.net 中有一个 table 来显示使用网格视图控件创建的帐户。

如您所见,我使用 RowDataBound 更改 header 颜色并隐藏编辑和删除。

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    Try
        For i As Integer = 0 To Me.GridView1.Rows.Count - 1
            If (TryCast(GridView1.Rows(i).FindControl("hflvl"), HiddenField).Value = 2) Then
                Me.GridView1.Rows(i).BackColor = Drawing.Color.LightBlue

                Me.GridView1.Rows(i).Cells(2).Text = ""
                Me.GridView1.Rows(i).Cells(3).Text = ""
            Else
                Me.GridView1.Rows(i).BackColor = Drawing.Color.White
            End If
        Next
    Catch ex As Exception
        'lblMsg.Text = ex.Message & ex.StackTrace
    End Try
End Sub

一切正常,下面是编辑代码

Protected Sub GridView1_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles GridView1.RowEditing
    GridView1.EditIndex = e.NewEditIndex
    PopTransDataGrid()
End Sub

Protected Sub GridView1_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
    GridView1.EditIndex = -1
    PopTransDataGrid()
End Sub

但是当我在一行中单击编辑时,编辑行下的所有行都采用默认样式,并且 rowdatabound 代码不生效。
即使是删除和编辑也显示为主要帐户。

这是我的 gridview 代码,

<asp:GridView ID="gvCustomerDeplst" runat="server" AutoGenerateColumns="False"
    DataKeyNames="AccountNo" Width="99%" BackColor="White" BorderColor="#CCCCCC"
    BorderStyle="Solid" BorderWidth="1px" CellPadding="4" ForeColor="Black" ShowFooter="True" PageSize="50" Font-Names="Verdana"
    Font-Size="11px">
    <AlternatingRowStyle BackColor="#f2f2f0" />
    <Columns>

        <asp:TemplateField HeaderText="AccountName" SortExpression="AccountName">
            <EditItemTemplate>
                <asp:TextBox ID="txtAccountName" runat="server" Height="19px" Text='<%# Bind("AccountName")%>' Width="200px"></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("AccountName")%>'></asp:Label>
                <asp:HiddenField ID="hflvl" Value='<%# Bind("lvl")%>' runat="server" />
            </ItemTemplate>
            <FooterStyle HorizontalAlign="Left" />
            <HeaderStyle HorizontalAlign="Left" />
            <ItemStyle HorizontalAlign="Left" />
        </asp:TemplateField>

        <asp:BoundField DataField="Category" HeaderText="Category" ReadOnly="True">
             <HeaderStyle HorizontalAlign="Left" />
             <ItemStyle HorizontalAlign="Left" />
        </asp:BoundField>

        <asp:TemplateField ShowHeader="False">
             <ItemTemplate>
                 <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="return confirm('Are you sure want to delete?')" Text="Delete"
                 CommandArgument='<%# Container.DataItemIndex %>' CommandName="Delete" />
             </ItemTemplate>
        </asp:TemplateField>

        <asp:CommandField ShowEditButton="True">
             <ItemStyle ForeColor="#0066FF" Width="100px" />
        </asp:CommandField>

        <asp:BoundField DataField="lvl" HeaderText="Lvl" ReadOnly="True">
             <HeaderStyle HorizontalAlign="Left" />
             <ItemStyle HorizontalAlign="Left" />
        </asp:BoundField>
    </Columns>
    <FooterStyle BackColor="#006699" ForeColor="White" />
    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Left" />
    <RowStyle BackColor="White" />
    <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F7F7F7" />
    <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
    <SortedDescendingCellStyle BackColor="#E5E5E5" />
    <SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>

如何在不丢失样式的情况下编辑gridview?

这是因为应用了条件样式,条件是 RowDataBound 事件中的 HiddenField 值为 2,但是,在 ASPX 页面中 HiddenField仅出现在 ItemTemplate 中而不出现在 EditItemTemplate 中,因此当数据绑定在 EditItemTemplate 中时,事件应用 Else 条件中的样式,因为它不能'找不到 HiddenField.

编辑:澄清 - HiddenField 必须在 EditItemTemplate 中,事件才能正确应用样式,例如基于您的 ASPX:

<EditItemTemplate>
  <asp:TextBox ID="txtAccountName" runat="server" Height="19px" Text='<%# Bind("AccountName")%>' Width="200px"></asp:TextBox>
  <asp:HiddenField ID="hflvl" Value='<%# Bind("lvl")%>' runat="server" />
</EditItemTemplate>