c# - How can I add a HyperLinkField to GridView databound to a file directory? -
i trying add hyperlinkfield column gridview pulls filenames file directory on server. i'm using code below, it's not working. it's showing path , filename, it's not clickable hyperlinkfield. i'm not sure i'm doing wrong, can me out?
public void getfilesandfolders() { using (irepentities dbcontext = new irepentities()) { string vchartempfilelocation = (from in dbcontext.tbl_applicationsetting a.vcharkey == "vchartempfilelocation" select a).single().vcharvalue; datatable gridviewsource = displayfilesingridview(); datarow gridviewrow; //get folders or directories , add in table directoryinfo directory = new directoryinfo(vchartempfilelocation); directoryinfo[] subdirectories = directory.getdirectories(); foreach (directoryinfo dirinfo in subdirectories) { gridviewrow = gridviewsource.newrow(); gridviewrow["claim"] = ddlclaimnumber.selecteditem; gridviewrow["name"] = dirinfo.name; dynamic newlink = new hyperlinkfield(); newlink = vchartempfilelocation + dirinfo.name; gridviewrow["link"] = newlink; gridviewrow["application"] = chkapplicationtype.selecteditem; gridviewsource.rows.add(gridviewrow); } //get files in directories fileinfo[] files = directory.getfiles("*.*", searchoption.alldirectories); foreach (fileinfo fileinfo in files) { gridviewrow = gridviewsource.newrow(); gridviewrow["claim"] = ddlclaimnumber.selecteditem; gridviewrow["name"] = fileinfo.name; dynamic newlink = new hyperlinkfield(); newlink = vchartempfilelocation + fileinfo.name; gridviewrow["link"] = newlink; gridviewrow["application"] = chkapplicationtype.selecteditem; gridviewsource.rows.add(gridviewrow); } gvbatchdetails.datasource = gridviewsource; gvbatchdetails.databind(); } } private datatable displayfilesingridview() { datatable dtgridviewsource = new datatable(); dtgridviewsource.columns.add(new datacolumn("claim", typeof(system.string))); dtgridviewsource.columns.add(new datacolumn("name", typeof(system.string))); dtgridviewsource.columns.add(new datacolumn("application", typeof(system.string))); dtgridviewsource.columns.add(new datacolumn("link", typeof(system.string))); return dtgridviewsource; } }
instead of building gridview manually, create datasource can bind to.
first create custom class hold details:
public class fileorfolderdetail { public string claim{get;set;} public string name{get;set;} public string link{get;set'} public string application{get;set;} }
next, create list
of new class:
var fileorfolderdetails = new list<fileorfolderdetail>();
at point, go through 2 loops you're doing, create new fileorfolderdetail
objects , add them list
.
finally, bind gridview
fileorfolderdetails
, , set hyperlinkfield
in aspx use link
datanavigateurl
.
let's start there , can tweak necessary.
Comments
Post a Comment