c# - 从 ASP.Net c# 中的 Gridview 控件中的文件夹获取文件名

标签 c# asp.net gridview

我需要将文件夹中的文件名显示到 GridView 控件中。

我想使用目录类

在我的数据库中,sFolder 列的每一行都有这个值:

control/Imp/foo

我试过这个tutorial在网络上,但我无法将文件名从文件夹获取到 GridView 控件中。

我没有错误,但即使文件夹路径正确,GridView 也是空的。

下面是我的代码。

你能帮帮我吗?

提前感谢您的帮助,非常感谢

.cs

dt2 = new DataTable();
ds2 = new DataSet();

sql = @String.Format(" SELECT * FROM tbl_2 WHERE sFolder IN ('control/Imp/foo'); ");

using (OdbcConnection cn =
  new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
    using (OdbcCommand cmd =
        new OdbcCommand(sql, cn))
    {

        OdbcDataAdapter adapter =
            new OdbcDataAdapter(cmd);
        adapter.Fill(ds2);

        if (ds2.Tables.Count > 0)
        {
            dt2 = ds2.Tables[0];
            FilePath = Server.MapPath("/myFolder/" + ds2.Tables[0].Rows[0]["sFolder"].ToString().Replace('/', '\\'));
            Response.Write(FilePath);

// the response write FilePath is C:\inetpub\wwwroot\aspnet\myFolder\control\Imp\foo // 

            string[] filesLoc = Directory.GetFiles(FilePath);
            List<ListItem> files = new List<ListItem>();
            foreach (string file in filesLoc)
            {
                files.Add(new ListItem(Path.GetFileName(file)));
            }
            gvDownload.DataSource = files;
            gvDownload.DataBind();

        }
    }
}

return ds2;

.aspx

<asp:GridView ID="gvDownload" EmptyDataText="Data empty"
    runat="server" AutoGenerateColumns="False"
    BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" GridLines="Vertical">
    <AlternatingRowStyle BackColor="#DCDCDC" />
    <Columns>
        <asp:BoundField DataField="Text" HeaderText="FileName" />
    </Columns>
</asp:GridView>

#编辑 01

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
    }


    private void BindData()
    {
        RetrieveProductsDowload();
    }

    private DataSet RetrieveProductsDowload()
    {
dt2 = new DataTable();
ds2 = new DataSet();

sql = @String.Format(" SELECT * FROM tbl_2 WHERE sFolder IN ('control/Imp/foo'); ");

using (OdbcConnection cn =
  new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
    using (OdbcCommand cmd =
        new OdbcCommand(sql, cn))
    {

        OdbcDataAdapter adapter =
            new OdbcDataAdapter(cmd);
        adapter.Fill(ds2);

        if (ds2.Tables.Count > 0)
        {
            dt2 = ds2.Tables[0];
            FilePath = Server.MapPath("/myFolder/" + ds2.Tables[0].Rows[0]["sFolder"].ToString().Replace('/', '\\'));
            Response.Write(FilePath);

// the response write FilePath is C:\inetpub\wwwroot\aspnet\myFolder\control\Imp\foo // 

            string[] filesLoc = Directory.GetFiles(FilePath);
            List<ListItem> files = new List<ListItem>();
            foreach (string file in filesLoc)
            {
                files.Add(new ListItem(Path.GetFileName(file)));
            }
            gvDownload.DataSource = files;
            gvDownload.DataBind();

        }
    }
}

return ds2;

}

最佳答案

请试试这个:

string[] allfiles = Directory.GetFiles(FilePath, "*", SearchOption.AllDirectories);
gvDownload.DataSource = allfiles;
gvDownload.DataBind();

关于c# - 从 ASP.Net c# 中的 Gridview 控件中的文件夹获取文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46682605/

相关文章:

asp.net - jquery通过客户端函数选择radiobuttonlist中的项目

c# - asp.net 在 c# 中使用 switch case for datetime.now.hour 格式

C# Gridview 标题样式不适用于排序

c# - 将小数从 LINQ 查询转换为货币

c# - 如何修复向多个收件人发送邮件时抛出的异常?

c# - sql server中的Datepart函数错误

c# - 计算文本文件中的行数

c# - 如何将 GWT 与 Visual Studio 结合使用来创建 ASP.NET 网站?

jquery - 如何将 CSS 类添加到 BoundField,以便我可以使用 jQuery 找到它?

c# - 如何知道控件(或窗口)何时在 WPF 中呈现(绘制)?