显示从数据库到gridview的多个值

时间:2014-12-18 10:49:03

标签: c# asp.net gridview

我有以下课程

public class FileDetails
{
public string FileName { get; set; }
public string FileDescription { get; set; }
public string FileLocation { get; set; }
}
public FileDetails() { }

public FileDetails(string fileName, string fileDescription, string fileLocation)
{
    FileName = fileName;
    FileDescription = fileDescription;
    FileLocation = fileLocation;
}

我有一个从DB读取这些值的函数。读取值后,我将它们显示在gridview上。我遇到的问题是显示的值,但FileName一遍又一遍地重复。我有5个不同的FileName存储在数据库中,而不是全部显示5个,它显示第一个FileName 5次。

读取值的函数如下所示

public FileDetails FilesInfo()
{
    string userName = HttpContext.Current.User.Identity.Name;

    using (SqlConnection connection = new SqlConnection(Common.ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT FileName,FileDescription,FileLocation FROM Files WHERE UserName=@UserName"))
        {
            cmd.Parameters.AddWithValue("UserName", userName);
            cmd.Connection = connection;
            connection.Open();
            using(SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    FileName = reader["FileName"].ToString();
                    FileDescription = reader["FileDescription"].ToString();
                    FileLocation = reader["FileLocation"].ToString();
                }
            }
        }
    }
    return new FileDetails(FileName,FileDescription,FileLocation);
}

Page_Load的另一个文件中,我有以下代码在gridview

上显示
 FileDetails info = new FileDetails();
    if (info.FilesInfo().FileLocation != null)
    {
        string[] filePaths = Directory.GetFiles(info.FilesInfo().FileLocation);

        List<FileDetails> files = new List<FileDetails>();
        foreach (string filePath in filePaths)
        {
            string filename = Path.GetFileName(filePath);
            files.Add(new FileDetails()
            {
                FileName = filename,
                FileDescription = info.FileDescription,
                FileLocation = filePath
            });
        }
        GridView1.DataSource = files;
        GridView1.DataBind();
    }
}

提前感谢您的所有帮助和支持

1 个答案:

答案 0 :(得分:1)

应该更改FilesInfo方法以返回FileDetails对象的集合,我在这里使用了通用列表,但您可以使用数据集,数据表或数组,或任何您喜欢的。像这样:

public List<FileDetails> FilesInfo()
{
string userName = HttpContext.Current.User.Identity.Name;
List<FileDetails> FileDetailsList = new List<FileDetails>();

using (SqlConnection connection = new SqlConnection(Common.ConnectionString))
{
    using (SqlCommand cmd = new SqlCommand("SELECT FileName,FileDescription,FileLocation FROM Files WHERE UserName=@UserName"))
    {
        cmd.Parameters.AddWithValue("UserName", userName);
        cmd.Connection = connection;
        connection.Open();
        using(SqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                FileDetails f = new FileDetails();
                f.FileName = reader["FileName"].ToString();
                f.FileDescription = reader["FileDescription"].ToString();
                f.FileLocation = reader["FileLocation"].ToString();

                FileDetailsList.Add(f);
            }
        }
    }
}
return FileDetailsList;
}

然后在你的页面加载中,你可以这样做:

List<FileDetails> info = FilesInfo();
        if(info.Count > 0)
        {
            GridView1.DataSource = info;
            GridView1.DataBind();
        }