Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Asp.net Display List of Files from Server Folder in Gridview

Jul 28, 2014
Introduction:

Here I will explain how to display list of files from server folder in asp.net Gridview or get files from folder or directory and bind to Gridview in asp.net using C#, VB.NET.

Description:

In previous articles I explained create zip files in asp.net, Delete files from uploaded folder in asp.net, create/delete directory in asp.net, Joins in SQL Server and many articles relating to Gridview, SQLjQueryasp.net, C#VB.NET. Now I will explain how to display list of files from server folder in asp.net Gridview using C#, VB.NET.

To display files from folder in Gridview we need to write the code like as shown below

C# Code


string[] filesPath = Directory.GetFiles(Server.MapPath("~/SampleFiles/"));
List<ListItem> files = new List<ListItem>();
foreach (string path in filesPath)
{
files.Add(new ListItem(Path.GetFileName(path)));
}
gvDetails.DataSource = files;
gvDetails.DataBind();
VB Code


Dim filesPath As String() = Directory.GetFiles(Server.MapPath("~/SampleFiles/"))
Dim files As New List(Of ListItem)()
For Each path__1 As String In filesPath
files.Add(New ListItem(Path.GetFileName(path__1)))
Next
gvDetails.DataSource = files
gvDetails.DataBind()

If you want to check it in complete example write the following code


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Get files from folder & bind to gridview in c#.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnGetFiles" Text="Get Files From Folder" runat="server" onclick="btnGetFiles_Click" />
<asp:GridView ID="gvDetails" CellPadding="5" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Text" HeaderText="FileName" />
</Columns>
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
Now in code behind add the following namespaces

C# Code


using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI.WebControls;
Once you add namespaces write the following code in code behind


// insert files in folder
protected void btnGetFiles_Click(object sender, EventArgs e)
{
BindGridview();
}
// Bind Data to Gridview
protected void BindGridview()
{
string[] filesPath = Directory.GetFiles(Server.MapPath("~/SampleFiles/"));
List<ListItem> files = new List<ListItem>();
foreach (string path in filesPath)
{
files.Add(new ListItem(Path.GetFileName(path)));
}
gvDetails.DataSource = files;
gvDetails.DataBind();
}
VB.NET Code


Imports System.Collections.Generic
Imports System.IO
Imports System.Web.UI.WebControls
Partial Class VBCode1
Inherits System.Web.UI.Page
' insert files in folder
Protected Sub btnGetFiles_Click(ByVal sender As Object, ByVal e As EventArgs)
BindGridview()
End Sub
' Bind Data to Gridview
Protected Sub BindGridview()
Dim filesPath As String() = Directory.GetFiles(Server.MapPath("~/SampleFiles/"))
Dim files As New List(Of ListItem)()
For Each path__1 As String In filesPath
files.Add(New ListItem(Path.GetFileName(path__1)))
Next
gvDetails.DataSource = files
gvDetails.DataBind()
End Sub
End Class
Demo

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

1 comments :

suganya said...

hello sir,ur blog was very usefull. I am expecting the article about Entity framework. ( eg. Gridview with entity framework in 3tier). Bcoz u r explaining clearly all concepts.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.