Click here to Skip to main content
16,004,727 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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.
Posted
Updated 6-Jul-11 18:22pm
v5
Comments
[no name] 6-Jul-11 22:33pm    
Format code snippets using the "code block" toolbar item

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
 
Share this answer
 
v3
Comments
harish85 7-Jul-11 2:58am    
Very Informative. Thanks.
Sergey Alexandrovich Kryukov 7-Jul-11 4:08am    
Thank you.
--SA
You can clone its data source like this:
C#
DataTable dtAll = dataGridViewAllElements.DataSource;
DataTable dtNew = dtAll.clone();
dataGridViewActiveElement.DataSource = dtNew;

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900