Search code examples
c#asp.netdatagridviewaspxgridview

need image in gridview from multiple folders


I have a project in which i am uploading image to folder croping the image and saving in another folder. Now i want to display both the images side by side in gridview please see my code below and help me.

ASPX Page

<asp:GridView ID="gvDetails" CellPadding="5" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:ImageField DataImageUrlField="Value" ControlStyle-Height="100" ControlStyle-Width="100" HeaderText="Real File" />
        <asp:ImageField DataImageUrlField="Value" ControlStyle-Height="100" ControlStyle-Width="100" HeaderText="Crop File" />
    </Columns>

    <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>

ASPX.CS Page

String path = HttpContext.Current.Request.PhysicalApplicationPath + "images\\";
String crop_path = HttpContext.Current.Request.PhysicalApplicationPath + "images\\crop\\";
protected void Page_Load(object sender, EventArgs e) {
    string[] filesPath = Directory.GetFiles(Server.MapPath("~/images/"));
    List<ListItem> files = new List<ListItem>();
    foreach (string path in filesPath) {
        string fileName = Path.GetFileName(path);
        files.Add(new ListItem(fileName, "~/images/" + fileName));
        files.Add(new ListItem(fileName, "~/images/crop/" + fileName));
    }
    gvDetails.DataSource = files;
    gvDetails.DataBind();
}

Solution

  • Assuming there are same number of images in both the folders, try this

    String path = HttpContext.Current.Request.PhysicalApplicationPath + "images\\";
    String crop_path = HttpContext.Current.Request.PhysicalApplicationPath + "images\\crop\\";
    

    Create a new class

    Class filenames
    {
        public string filename { get; set; }
        public string crop_filename { get; set; }
    }
    

    and then in page load

    protected void Page_Load(object sender, EventArgs e)
    {
        string[] filesPath = Directory.GetFiles(Server.MapPath("~/images/"));
        string[] crop_filesPath = Directory.GetFiles(Server.MapPath("~/images/crop/"));
        List<filenames> files = new List<filenames>();
        filenames objfilenames;
        for(int i=0; i<filesPath.length; i++)
        {
            objfilenames = new filenames();
            objfilenames.filename = "~/images/" +  Path.GetFileName(filesPath[i]);
            objfilenames.crop_filename = "~/images/crop/" + Path.GetFileName(crop_filesPath[i]);
            files.Add(objfilenames);
        }
        gvDetails.DataSource = files;
        gvDetails.DataBind();
    }