开发者

C# Events and Control Interaction

开发者 https://www.devze.com 2023-02-04 07:17 出处:网络
I am currently refactor开发者_StackOverflowing some of my old code that is pretty terrible.I will have a class that creates a Treeview, populates the node, etc and is displayed on a Winform.

I am currently refactor开发者_StackOverflowing some of my old code that is pretty terrible. I will have a class that creates a Treeview, populates the node, etc and is displayed on a Winform. Each node on the Treeview represents some data and when the user clicks on that then a datagridview is also displayed on the Winform. The datagridview will be generated within a new class also.

My idea on this is, when the Winform Loads, create and display the Treeview and use an event to monitor for node clicks. When the Winform handles such an event, then it creates the datagrid object and diplays that.

IS this the best way to architect this?

Thanks.


Yes, what you describe is a standard way to do things in WinForms.

You don't need to create the DataGridView every time though - just place it on the Form and in the event handler load the data from somewhere and change the contents of the DataGridView by assigning to DataGridView.DataSource.

In the form designer, define columns for DataGridView and set their DataPropertyName to the names of the corresponding properties of the data objects in the collection.

Note: the data assigned to the DataGridView.DataSource can be a collection of objects or also a DataTable if you are using ADO.NET to read the objects from a database.


Just use the TreeView.AfterSelect event. It fires anytime the user selects another node, either by keyboard or mouse. Be sure to dispose the old DGV if you replace it completely.

0

精彩评论

暂无评论...
验证码 换一张
取 消