我正在尝试将一个超链接字段列添加到GridView,该文件从服务器上的文件目录中提取文件名。我正在使用下面的代码,但它并不完全正常。它显示路径和文件名,但它不是可点击的超链接字段。我不确定我做错了什么,有人可以帮帮我吗?
public void GetFilesAndFolders()
{
using (IREPEntities dbContext = new IREPEntities())
{
String vcharTempFileLocation = (from a in dbContext.tbl_ApplicationSetting
where a.vcharKey == "vcharTempFileLocation"
select a).Single().vcharValue;
DataTable gridviewSource = DisplayFilesInGridView();
DataRow gridviewRow;
//Get All Folders Or Directories and 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 all 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;
}
}
答案 0 :(得分:0)
不是手动构建GridView,而是创建可绑定的数据源。
首先创建一个自定义类来保存详细信息:
public class FileOrFolderDetail
{
public string Claim{get;set;}
public string Name{get;set;}
public string Link{get;set'}
public string Application{get;set;}
}
接下来,创建新班级的List
:
var fileOrFolderDetails = new List<FileOrFolderDetail>();
此时,像你一样经历你的两个循环,但是创建新的FileOrFolderDetail
个对象并将它们添加到List
。
最后,将您的GridView
绑定到fileOrFolderDetails
,并在ASPX中设置HyperLinkField
以使用Link
作为DataNavigateUrl
。
让我们从那里开始,我们可以根据需要进行调整。