Show files from 2 different folders in a single Gridview

て烟熏妆下的殇ゞ 提交于 2019-12-31 05:25:13

问题


Is it possible to show files from 2 different folders (c:\test1 and c:\test2) in the same gridview?

I work in VB.net (VS 2010)

Thanks!


回答1:


Yes. Get list of all the files using Directory.GetFiles() into a single IEnumerable<string> and bind it to a GridView.

This is how you'll do it in c#.

            List<string> allFiles = new List<string>();
            allFiles.AddRange(Directory.GetFiles(@"C:\test1\*"));
            allFiles.AddRange(Directory.GetFiles(@"C:\test2\*"));

            yourGV.DataSource = allFiles;
            yourGV.DataBind();



回答2:


Try something like this:

Dim files As New List(Of String)()
files.AddRange(GetAllFilesFromDir("C:\foo")) 
files.AddRange(GetAllFilesFromDir("C:\bar"))
'GetAllFilesFromDir() must return IEnumerable string
gv.DataSource = files
gv.DataBind()

<asp:gridview ID="gv" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="file" runat="server" Text='<%# Container.DataItem %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:gridview>

You hadn't shown your code in your question, so the above example demonstrates how this might be done generally.




回答3:


yes. Add them both as collections to List() or any other collection type. Then bind that set to the gridview.



来源:https://stackoverflow.com/questions/6309182/show-files-from-2-different-folders-in-a-single-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!