【问题标题】:DataBinding DataGridView List of Lists c# complex Collectionsc#复杂集合DataBinding DataGridView List of Lists
【发布时间】:2012-09-25 07:42:30
【问题描述】:

我正在尝试将page 类列表绑定到datagridview

class Page : INotifyPropertyChanged
{
    public List<Tuple<DateTime, String>> Lines { get; set; }
    public Color c { get; set; }
    public String filePath { get; set; }

//rest of class code...
}
//on the 'Form1' class
BindingList<Page> pages = new BindingList<Page>();

我希望datagridview 中的一行代表列表中的一行

List<Tuple<DateTime, String>> Lines

一列用于DateTime,一列用于对应的string

每一行都应与color 协调到它所属的page

我正在尝试绑定它,因为我希望通过源文件的更新实时更新 GUI。

我的实现已经循环了好几天,任何帮助/建议将不胜感激。谢谢!

编辑:一些示例数据:

20-Apr-11 08:36:44.312   Start       I *** C:\Cromos 3.0\toolset\Ntbin\Release\crm_gui_gtm.exe on BENJAMIN-PC - release - cromos: build 2780, Gui version: 400, File version: 80 ***
20-Apr-11 08:36:44.312   symbol element total: 9

【问题讨论】:

  • 所以...如果你有 3 个Pages,每个有 2 个Lines - 那总共是 6 行?
  • 没错,一些样本数据:20-Apr-11 08:36:44.312 开始,20-Apr-11 08:36:44.312 符号元素总数:9
  • 好吧,将其扁平化为一个列表很容易做到,但是:坦率地说,在保留更改通知(在列表和项目上)的同时做到这一点太难了。我建议:保持简单。
  • 我已经有了使用文件系统观察器并输出到列表视图的功能。但是 datagridview 提供了内置排序,这将使实现更容易、更简单、更快……如果我能弄清楚的话!

标签: c# list data-binding collections datagridview


【解决方案1】:

对于查看此问题的任何人,我通过更改数据结构解决了问题。我为单行创建了一个类,如下所示:

    class Line : INotifyPropertyChanged
{
    public Color _c;
    public DateTime _dateTime;
    public String _comment;
    public event PropertyChangedEventHandler PropertyChanged;
}

然后实现了一个BindingList 来存储所有的行并按照 Vikram 链接的示例进行操作。

        BindingList<Line> all = new BindingList<Line>();

另外,请确保您在表单初始化程序中包含这一行,而不是像我一样的方法!

            dataGridView1.DataSource = all;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    相关资源
    最近更新 更多