如何将pageddatasource与datatable和没有sql语句一起使用

时间:2015-02-03 04:22:02

标签: asp.net vb.net

我没有使用任何sql表,但我想插入带有datatable的pageddatasource。如果可能的话。

这是我的代码

Private Sub GenerateDownloadLinks()
    Dim path__1 As String = Server.MapPath("..\Fileupload\insert\")
    If Directory.Exists(path__1) Then
        Dim ShowContent As New DataTable()
        ShowContent.Columns.Add("Image", GetType(String))
        ShowContent.Columns.Add("DownloadLink", GetType(String))
        ShowContent.Columns.Add("FileName", GetType(String))
        Dim di As New DirectoryInfo(path__1)
        For Each fi As FileInfo In di.GetFiles()
            Dim dr As DataRow = ShowContent.NewRow()
            dr("FileName") = fi.Name
            dr("DownloadLink") = Server.MapPath("..\Fileupload\insert\") + fi.Name
            Dim ext As String = Path.GetExtension(fi.FullName)
            Select Case ext
                Case ".rtf"
                    dr("Image") = ResolveUrl("..\Fileupload\images\word.png")
                    Exit Select
                Case ".np"
                    dr("Image") = ResolveUrl("..\Fileupload\images\notepad.png")
                    Exit Select
                Case ".txt"
                    dr("Image") = ResolveUrl("..\Fileupload\images\text.png")
                    Exit Select
                Case ".jpg"
                    dr("Image") = ResolveUrl("..\Fileupload\images\image.png")
                    Exit Select
                Case ".png"
                    dr("Image") = ResolveUrl("..\Fileupload\images\img.png")
                    Exit Select
                Case ".gif"
                    dr("Image") = ResolveUrl("..\Fileupload\images\mp.png")
                    Exit Select
            End Select
            ShowContent.Rows.Add(dr)
        Next
        Dim ds As DataSet = New DataSet()
        ds.Tables.Add("ShowContent")
        Dim pds As New PagedDataSource()
        pds.DataSource = ShowContent
        pds.AllowPaging = True
        pds.PageSize = 9
        totalpage = Math.Round(pds.DataSourceCount / 9)
        pds.CurrentPageIndex = currentpageindex
        datalist.DataSource = pds
        datalist.DataMember = "ShowContent"
        datalist.DataBind()
    End If
 End Sub 

我收到的错误是:

Unable to cast object of type 'System.Data.DataTable' to type 'System.Collections.IEnumerable'.

谢谢,

1 个答案:

答案 0 :(得分:0)

您需要将PagedDataSource的数据源从DataTable更改为DataSet。因为Datasource属性需要IEnumerable对象。

  pds.DataSource = ds.defaultview ;