开发者

Displaying same datagridview in different places (c#)?

I'm working on a desktop application in C# (in Visual Studio 2010, framework 4.0). I have to display same (or slightly modified - without some columns) datagridview / table in different places. I'll get the data from querying different (local) files. I find it very consuming to make a new query for each tabl开发者_Go百科e. Thus I thought about binding tables with query. But I don't know if it is the best way to do it. I don't know how "expensive" it is to bind data to gridview. Maybe it would be better to somehow show the same table, maybe put it in a panel an then display it properly (moving it to different positions/user controls according to the user's navigation).

These tables are not that big, but I'd need to display 4-5 of them in quite some locations and I don't want to waste memory with them because there are also some very large tables.

Any ideas?

Thank you for your time and answers.


As Ken already stated you should put several DataGridViews into your form. But you attach them all to the same BindingSource. And to the BindingSource.DataSource you simply attach a list (collection, whatever) containing all the data after you grab it once.

The setup and configuration of all data grids can be done at design time with the Visual Studio designer. To do this you should take a look into this answer. It explains how to create and a attach a binding source to a TextBox, but for a data grid is the same. After this binding in the designer you can afterwards dive into the Columns property of the grid with the designer and change the visibility, etc. of each column as you like.


Having a single table to which each datagridview is databound is probably the best approach. This allows you to load data one time (which is great), and then have each individual datagridview "interpret" the data in it's one way. Sure save on a lot of file I/O!


You can cache the object of grid in memory (avoid dispose, when container is disposed) and show on other forms. Better approach will be if you cache the data not UI controls. And use the data on the grid on different forms.


If you want to minimize the memory needed and at the same time want to execute your queries only once, you could save the results of those queries into (for example) datatables. Then create a datagridview and bind it on those tables every time you need one.

If you want to really keep it small, you can define your own custom data classes This link explains which interfaces you need to use for those custom classes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜