如何绑定List<>到dataGridview c#

本文关键字:dataGridview List 何绑定 绑定 | 更新日期: 2023-09-27 17:54:20

我是windows应用程序的新手。
我写了一个代码来读取文件夹内的文件,逐行读取,并找到一些字符串,并显示其计数到dataGridView。

我想填充一个不工作的dataGridview。
我在网上试过不同的方法,但都不奏效。

我知道我可能犯了一个愚蠢的错误。
我的代码如下

  public class FileDetail
    {
        public  string filename;
        public int openConnectionCount;
        public int closeConnectionCount;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int openConnectionCount = 0;
        int closeConnectionCount = 0;
        List<FileDetail> fileDetails =new List<FileDetail>();
        foreach (string file in Directory.EnumerateFiles(textBox1.Text, "*.*", SearchOption.AllDirectories))
        {
            FileDetail fileDetail =new FileDetail();
            fileDetail.filename = file;
            // Read the file as one string.
            using (StreamReader myFile = new StreamReader(file))
            {
                string line;
                while ((line = myFile.ReadLine()) != null)
                {
                    if (line.Contains(" OpenConnection()")) openConnectionCount++;
                    if (line.Contains(" CloseConnection()")) closeConnectionCount++;
                }
                fileDetail.openConnectionCount = openConnectionCount;
                fileDetail.closeConnectionCount = closeConnectionCount;
            }
            fileDetails.Add(fileDetail);
            openConnectionCount = 0;
            closeConnectionCount = 0;
        }

        dataGridView1.AutoGenerateColumns = false;
        dataGridView1.Columns.Add("filename", "File Name");
        dataGridView1.Columns.Add("openConnectionCount", "Open Connection");
        dataGridView1.Columns.Add("closeConnectionCount", "close Connection");
        dataGridView1.Columns[0].DataPropertyName = "filename";
        dataGridView1.Columns[1].DataPropertyName = "openConnectionCount";
        dataGridView1.Columns[2].DataPropertyName = "closeConnectionCount";
        var filenamesList = new BindingList<FileDetail>(fileDetails); // <-- BindingList
        dataGridView1.DataSource = filenamesList;

        //dataGridView1.DataSource = fileDetails;


        //dataGridView1.b
        //foreach (var fileDetail in fileDetails)
        //{
        //    label1.Text += "FileName:=" + fileDetail.filename + "'t Open Connection:= " +
        //                   fileDetail.openConnectionCount + "'t close connection:=" +
        //                   fileDetail.closeConnectionCount+"'n";
        //}
    }

编辑:-

我已经跟随了一些相同的链接
如何绑定列表到dataGridView?
http://social.msdn.microsoft.com/forums/eu/csharplanguage/thread/766c7b46 - 6431 - 4 - ec1 - 8 e74 - 50 - f718c6606b

如何绑定List<>到dataGridview c#

数据绑定只适用于Set/Get属性,不适用于公共字段。

试着像这样改变你的FileDetail:

public class FileDetail
{
    public  string filename { get; set; }
    public int openConnectionCount { get; set; }
    public int closeConnectionCount { get; set; }
}

如果你想自动生成datagridview的列,那么你可以使用它作为

public class FileDetail
{
    [DisplayName("File Name")]    
    public  string filename { get; set; }
    [DisplayName("Open Connection")]    
    public int openConnectionCount { get; set; }
    [DisplayName("close Connection")]    
    public int closeConnectionCount { get; set; }
}

可以使用列表作为数据源而不需要绑定源

dataGridView1.DataSource = fileDetails.ToArray();

要了解更多信息,请查看此链接将类对象作为数据源绑定到DataGridView