如何将列从数据网格复制到另一个? [英] How to copy columns from data grid to another?

查看:124
本文介绍了如何将列从数据网格复制到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个数据网格,并希望所有其他数据网格具有相同的列

这不起作用:

Hi All,

I have one datagrid and want all other datagrids to have the same columns

This is not working:

dataGridViewActiveElement.Columns = dataGridViewAllElements.Columns;



连这个都不行:



Even this one is NOT woking:

foreach (DataGridViewColumn dgvc in dataGridViewAllElements.Columns)
{
    dataGridViewActiveElement.Columns.Add(dgvc);
}



这是最好的方法吗?

请指导我.



Is this the best way ?

Please guide me.

Thanks in Advance.

推荐答案

主要原理是:不要将数据从一个控件复制到另一个控件.

而是创建一个数据层.从数据填充所有控件.当控件事件应该更改某些数据时,请在数据层上进行操作,然后向所有控件使用的其他控件发送通知,以重新填充或相应地调整数据表示.也就是说,您的控件应该扮演侦听器可观察数据结构的角色.即使您没有明确介绍这些概念(尽管建议这样做),代码的设计也应遵循这种或类似的模式.

我建议您学习并分析以下建筑模式的适用性( http://en.wikipedia .org/wiki/Architectural_pattern_(computer_science) [^ ]):

MVVM —模型视图视图模型,
http://en.wikipedia.org/wiki/Model_View_ViewModel [> http://en.wikipedia.org/wiki/Model-view-controller [ ^ ])),

MVA —模型视图适配器,
http://en.wikipedia.org/wiki/Model–view–adapter [ ^ ],

MVP —模型视图呈现器,
> http://en.wikipedia.org/wiki/Model-view-presenter [ ^ ].
请注意这些架构的动机.如果您了解它,就可以提出更好的设计思路.

—SA
Main principle is this: don''t copy data from one control to another one.

Instead, create a data layer. Populate all controls from data. When a control event is supposed to change some data, do it on your data layer, then send a notifications to other controls used by all controls to re-populate or adjust data presentation accordingly. That means, you control should play the role of listeners or the observable data structure. Even if you don''t introduce those concept explicitly (which is recommended though), the design of the code should follow this or similar pattern.

I suggest you learn and analyze applicability of the following architectural patterns (http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)[^]):

MVVM — Model View View Model,
http://en.wikipedia.org/wiki/Model_View_ViewModel[^],

MVC — Model-View-Controller,
http://en.wikipedia.org/wiki/Model-view-controller[^]),

MVA — Model-View-Adapter,
http://en.wikipedia.org/wiki/Model–view–adapter[^],

MVP — Model-View-Presenter,
http://en.wikipedia.org/wiki/Model-view-presenter[^].
Pay attention for the motivation of those architectures. If you understand it, you would be able to create better design ideas.

—SA


您可以克隆其数据源,如下所示:
You can clone its data source like this:
DataTable dtAll = dataGridViewAllElements.DataSource;
DataTable dtNew = dtAll.clone();
dataGridViewActiveElement.DataSource = dtNew;


不用担心,数据不会被复制,只会被模式复制.


Don''t worry, the data won''t be copied, just the schema.


这篇关于如何将列从数据网格复制到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆