Show files from 2 different folders in a single Gridview

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!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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();

Method 3

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x