Introduction:
Here
I will explain how to 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, SQL ,jQuery,asp.net, C#,VB.NET. Now I will explain how
to get files from folder and bind to Gridview in asp.net using C#, VB.NET.
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. |
|||
|
|||
17 comments :
it's amazing...thank u Mr.suresh
hi Suria...
kemnu chhe lya??? :p
suresh sir please keep attachment of code for every program so tat we can download the code..
Nice
hello suresh
please provide c# code for uploading all files in selected folder. means all files should get uploaded to database when a folder is selected.
thanks in advance
hello suresh
i want a code that we browse and upload a file to gridview in all format
Hi suresh..I tried...and awesome.. but how to make it can be opened or linked..Thanks..
How to insert, update and delete data into gridview
using jQuery, c#?
Hi suresh ....
Its realy nice
How to display all folders from my system in asp.net gridview ?.
ex:- D:\ drive all folders should be display on webpage.
Hi suresh ur article is very helpful. But one thing in my mind that if user wants to upload files on different folder i.e pdf file should go in pdf folder or image file in image folder.
Reply soon.
Honey chawla
Can we Get Files from Folder / Directory & bind from other website location...???
Like this...
string[] filesPath = Directory.GetFiles("http://www.AnyDomainName.com/Images/");
If yes
Please help me....I spent lots of time for this query.
M*
u*
j*
l*
g*
this output display using gridview
urgent code send to me
I am getting error plz help me, I am news here
HTTP Error 403.14 - Forbidden
hi i need help . how bind local folder names as tree view. now i get list of local folder name and bind to tree view that all bind has like parent list only . but i need fist folder and in that folder list of folder list . parent to child as like. any help me
Nice Tutorial.I need one help from you.How to get the files from folder (ie., upload and download) .Not in server folder.The file accessing in difference drives like d,e, or f .
How do I make the files previewable/downloadable? Thanks!
Note: Only a member of this blog may post a comment.