0

我正在尝试将超链接字段列添加到从服务器上的文件目录中提取文件名的 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;
    }


}
4

1 回答 1

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到,并在 ASPX 中fileOrFolderDetails设置你作为.HyperLinkFieldLinkDataNavigateUrl

让我们从那里开始,我们可以根据需要进行调整。

于 2013-08-15T20:16:23.580 回答