Gridview bound to filenames in directory
I'm having some evils trying to get my GridView control to behave. I have the below code, which successfully displays all the files in the directory. However I require two changes, both of which I am struggling with:
a) Currently the URL you get when clicking on the URL field is http://localhost/LBSExplorer/SharedUser.csv (ie my home directory with the filename).
What I require is that the 'Display Text' be the filename only, and the URL be my desired text followed by the filename eg: http://mystuff/page.aspx?FileID=SharedUser.csv
b) I want only to see the files that start with a certain prefix eg "Pay". I can do that with something like: string[] filelist = Directory.GetFiles((@"C:\MF\Data\","Pay*.*"); but this doesn't like to bind to my Gridview!
I'd appreciate your help!
Mark
const string DocumentFolderPhysicalPath = (@"C:\MF\Data\");
const string DocumentFolderUrl = (@"C:\MF\Data\"); //"http://localhost/virtualfolderna开发者_JS百科meyouexposed/"; ; // now it is hardcoded but you could retreive it automatically
HyperLinkField hyperLinkField = new HyperLinkField();
hyperLinkField.DataTextField = "Name";
hyperLinkField.DataNavigateUrlFields = new string[] { "Name" };
//Would like this to work!
//HyperLinkField hyperLinkField2 = new HyperLinkField();
//hyperLinkField2.DataTextField = "Destination";
//hyperLinkField2.DataNavigateUrlFields = new string[] { (@"C:\MF\Data\") + "Name" };
GridView1.DataSource = GetDocuments(DocumentFolderPhysicalPath);
GridView1.Columns.Add(hyperLinkField);
GridView1.DataBind();
private System.IO.FileInfo[] GetDocuments(string physicalPath)
{
System.IO.DirectoryInfo directory =
new System.IO.DirectoryInfo(physicalPath);
if (directory.Exists)
{
return directory.GetFiles();
}
else
{
throw new System.IO.DirectoryNotFoundException(physicalPath);
}
}
What you are looking for is the DataNavigateUrlFormatString property.
hyperLinkField.DataNavigateUrlFormatString = "http://mystuff/page.aspx?FileID={0}";
The {0} here is replaced with your DataNavigateUrlFields value.
精彩评论