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 Serverand 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.
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
No comments:
Post a Comment