connectseekers.com

Tuesday, 16 August 2016

Show Images in Gridview from Folder in Asp.net

8/16/2016 - By Pranav Singh 0

This article will show you how you how you can detect the images present in folder and bind the detected images to gridview in asp.net using c#.net.


So for this article first we will create a new asp.net article and add an image folder.


Now we will add a gridview control and add BoundField, TemplateField. In TemplateField we will add the image control and bind it with the Eval.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm12.aspx.cs" Inherits="WebApplication7.WebForm12" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Show Images in Gridview From Folder in Asp.net Uisng C#</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="FileName" HeaderText="Image Name" />
                    <asp:TemplateField HeaderText="Image">
                        <ItemTemplate>
                            <img src='<%#Eval("FilePath") %>' width="100px" height="100px"></img>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>

Now check the below code.

public DataTable GetFileDetail()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("FileName");
            dt.Columns.Add("FilePath");
            string folderPath = @"\Images\";
            //Read all files from folder
            string[] filePaths = Directory.GetFiles(Server.MapPath(folderPath));

            foreach (string item in filePaths)
            {
                string filename = Path.GetFileName((item));
                //Add file detail to datatable
                DataRow dataRow = dt.NewRow();
                dataRow["FileName"] = filename;
                dataRow["FilePath"] = folderPath + filename;
                dt.Rows.Add(dataRow);
            }
            return dt;
        }

In above code I have prepared the datatable and added the field “FileName” and “FilePath”. After that I have detected all the files present in a directory. After that I have added the data to the datatable.

Now check the code to bind the gridview control.

DataTable dt = new DataTable();
            dt = GetFileDetail();
            GridView1.DataSource = dt;
            GridView1.DataBind();

Now here is the complete code of the page.

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication7
{
    public partial class WebForm12 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = GetFileDetail();
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        public DataTable GetFileDetail()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("FileName");
            dt.Columns.Add("FilePath");
            string folderPath = @"\Images\";
            //Read all files from folder
            string[] filePaths = Directory.GetFiles(Server.MapPath(folderPath));

            foreach (string item in filePaths)
            {
                string filename = Path.GetFileName((item));
                //Add file detail to datatable
                DataRow dataRow = dt.NewRow();
                dataRow["FileName"] = filename;
                dataRow["FilePath"] = folderPath + filename;
                dt.Rows.Add(dataRow);
            }
            return dt;
        }
    }
}

Now we have done run the application to check the output.


DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top