开发者

Sort nested gridview that is bound to outer gridview dataitem

开发者 https://www.devze.com 2023-01-29 11:54 出处:网络
Not able to get sorting to work. Error: Object variable or With b开发者_开发知识库lock variable not set.

Not able to get sorting to work.

Error: Object variable or With b开发者_开发知识库lock variable not set.

Markup

<asp:GridView ID="gvSpecList" runat="server" AutoGenerateColumns="false" ShowHeader="false" OnRowEditing="gvSpecList_OnRowEditing" OnRowCancelingEdit="gvSpecList_OnRowCancelingEdit">
     <AlternatingRowStyle BackColor="#ebe9e2" />
         <Columns>
             <asp:TemplateField ItemStyle-Width="20" ItemStyle-VerticalAlign="Top">
                 <ItemTemplate>
                     <asp:ImageButton ID="btnOpenDetail" runat="server" ImageUrl="/Images/plus.png" CommandName="edit" />
                 </ItemTemplate>
                 <EditItemTemplate>
                     <asp:ImageButton ID="btnCloseDetail" runat="server" ImageUrl="/Images/minus.png" CommandName="cancel" />
                 </EditItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField>
                 <ItemTemplate>
                     <span style="font-size:13px; font-weight:bold;"><asp:Label ID="Label1" runat="server" Text='<%# Container.DataItem("specialty") %>'></asp:Label></span>
                 </ItemTemplate>
                 <EditItemTemplate>
                     <table>
                         <tr>
                             <td><span style="font-size:13px; font-weight:bold;"><asp:Label ID="Label1" runat="server" Text='<%# Container.DataItem("specialty") %>'></asp:Label></span></td>
                         </tr>
                         <tr>
                             <td>
                                 <asp:GridView ID="gvElectives" AllowSorting="true" AutoGenerateColumns="false" runat="server" DataSource='<%#Container.DataItem.Row.GetChildRows("dr_specialty") %>' GridLines="None" OnSorting="gvElectives_Sorting">
                                     <Columns>
                                         <asp:TemplateField HeaderText="Title" SortExpression='elective_title'>
                                             <ItemTemplate>
                                                 <asp:Label runat="server" Text='<%# Container.DataItem("elective_title") %>'>
                                                 </asp:Label>
                                             </ItemTemplate>
                                         </asp:TemplateField>
                                     </Columns>
                                 </asp:GridView>
                             </td>
                         </tr>
                     </table>
                 </EditItemTemplate>
             </asp:TemplateField>
         </Columns>
     </asp:GridView>

Code Behind

Private Property GridViewSortDirection() As String
    Get
        Return If(TryCast(ViewState("SortDirection"), String), "ASC")
    End Get
    Set(ByVal value As String)
        ViewState("SortDirection") = value
    End Set
End Property

Private Property GridViewSortExpression() As String
    Get
        Return If(TryCast(ViewState("SortExpression"), String), String.Empty)
    End Get
    Set(ByVal value As String)
        ViewState("SortExpression") = value
    End Set
End Property

Private Function GetSortDirection() As String
    Select Case GridViewSortDirection
        Case "ASC"
            GridViewSortDirection = "DESC"
            Exit Select
        Case "DESC"
            GridViewSortDirection = "ASC"
            Exit Select
    End Select
    Return GridViewSortDirection
End Function

Protected Function SortDataTable(ByVal dataTable As DataTable) As DataView
    If dataTable IsNot Nothing Then
        Dim dataView As New DataView(dataTable)
        If GridViewSortExpression <> String.Empty Then
            dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GetSortDirection())
        End If
        Return dataView
    Else
        Return New DataView()
    End If
End Function

Protected Sub gvElectives_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
    Dim gv As GridView = DirectCast(sender, GridView)
    GridViewSortExpression = e.SortExpression
    gv.DataSource = SortDataTable(TryCast(gv.DataSource, DataTable))
    gv.DataBind()
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消