0

我们有一个由 Visual Studio 数据集设计器创建的类型化数据集。

你能告诉我如何从类型化的 DataSet 创建一个 DataView 对象吗?我们想要更改它的排序,以便它可以显示在 ASP.Net DataView 中。

我们搜索了很长时间才找到这个,但空手而归。

这是 DataView 的标记。由于我们的 Web 表单很大,所有编码都被减少了:

<asp:UpdatePanel 
    ID="UpdatePanelSummary" 
    runat="server" 
    UpdateMode="Always">

    <ContentTemplate> 

        <h1>Maintenance</h1>

        <% '-- GridView (Grid) for summary.                                                      -- %>
        <% '-- The user chooses a summary row from here and details are shown in a DetailsView.  -- %>
        <% '--------------------------------------------------------------------------------------- %>

        <asp:GridView
            ID="GridViewSummary" 
            runat="server" 
            AllowSorting="True" 
            AutoGenerateColumns="False" 
            DataKeyNames="ID" 
            Width="224px" 
            AllowPaging="True" 
            PageSize="7">

            <Columns>
                <asp:BoundField DataField="Unit" HeaderText="Unit" 
                    SortExpression="Unit" />

                <asp:BoundField DataField="TheName" HeaderText="Name" 
                    SortExpression="TheName" />

                <asp:BoundField DataField="ID" 
                    HeaderText="ID" SortExpression="ID" InsertVisible="False" ReadOnly="True" 
                    Visible="False" />

                <asp:CommandField ButtonType="Button" SelectText="Select Unit Details" 
                    ShowSelectButton="True" />
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

这是我们要从代码隐藏文件中创建到 DataView 中的数据:

' Database objects.
'------------------
Dim theTableAdapter As New DataSetClassesTableAdapters.ClassesTableAdapter

Private Sub Teachers_Init(sender As Object, e As EventArgs) Handles Me.Init

    ' Load the data from the database into the GridView.
    '---------------------------------------------------
    GridViewSummary.DataSource = theTableAdapter.GetDataByAllClasses
    GridViewSummary.DataBind()

End Sub

Private Function bindgrid() As DataView

    Dim dv As DataView = New DataView()

    If ViewState("sortExpr") IsNot Nothing Then
        dv = New DataView("The DataSet Is Here")
        dv.Sort = DirectCast(ViewState("sortExpr"), String)
    Else
        dv = ds.Tables(0).DefaultView
    End If

    Return dv
End Function

Protected Sub GridViewSummary_Sorting(sender As Object, e As GridViewSortEventArgs) Handles GridViewSummary.Sorting

    ViewState("sortExpr") = e.SortExpression
    GridViewSummary.DataSource = bindgrid()
    GridViewSummary.DataBind()
End Sub
4

1 回答 1

0

DataView 需要连接到为其提供排序的 DataTable。

if ViewState(...) IsNot Nothing then
    dv = New DataView()
    dv.Table = ds.Tables(0)
    ....
else

end if

顺便说一句,没有仅将字符串作为输入参数的DataView 构造函数

于 2012-12-11T16:49:14.020 回答