Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, in the process of designing and implementing a software system, I have been running into various performance issues. As a result, I am trying to figure ways to minimize memory usage and increase reading and writing performance. I stumbled upon the possibilities of using a DataTable for storing and handling parts of my database; however, I am not quite familiar with it, and its association with DataGridView, so I am asking for some pointers in this area.

What I want to do is to have a single large DataTable. I want to display certain columns of the table in a DataGridView - different columns in each DataGridView. If I change the contents of the DataTable, the changes will be reflected back in all the DataGridView.

My trouble is that I can't figure out three things:

1. Will a change on DataTable be reflected as similar change on all associated DataGridView?

2. How do I filter out certain columns of DataGridView when the contents are coming from DataTable? Is it doable only with using DataView.ToTable() because it creates a copy of the table; thus, using more memory?

3. Can I set the last column as hidden in each of the DataGridView?
Posted

If you bind a datatable to datagridview as datasource, the changes you made on datatable also occurs on datagridview. I don't know if there is a short way, but you can code a method in order to filter a specific field of datatable. Please check
C#
DataRow.Field(string name)
method.
 
Share this answer
 
v2
Yes, any change within the datatable will be reflected on the DataGridView's(DGV) once the control goes through a databind method (postback, or manual call if using ajax).

The easiest way that I've seen to filter the columns is to set the AutoGenerateColumns to false and then create the columns you want within the Columns container of the DGV.You can still use bound fields, you will just have to define them manually. You can also do this from codebehind if you need it to be dynamic.

You then have total control over what's visible and what's not. All you have to do is set the Visible option on the column entry. Also, if you will be using the built in edit/delete buttons you will need to set the DataKeyNames to the column name that holds the row's ID entry.
 
Share this answer
 
Comments
Que Trac 30-Nov-11 19:31pm    
Thanks, umutarge and Leeland. The change is reflected back just what I needed.

So I need to set AutoGenerateColumns to false. That was what giving me trouble. By matching up the DataPropertyName in DataGridViewTextBoxColumn with the name used for the columns in DataTable, I am able to filter out the columns I needed for each DataGridView. And I can show or hide a DataGridViewTextBoxColumn on DataGridView using the Visible property as Leeland has mentioned.

I spend quite a bit of time googling for the answers before I gave up and posted the question here. Thank you so much.

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