Link to home
Start Free TrialLog in
Avatar of kdunnett
kdunnett

asked on

Hide datagrid columns when there is no data in the column

Good night all,

I have a datagrid that gets binded by some dyamic query.  There will be times when there are full columns in the datagrid that won't have any data.  Is there a way I can loop through a datagrid, say pick a column and check all rows to see if there is any data, if not, make the column visable = false and then go to next column?

Thanks,
Kris
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

In order to hide a DataGrid column for 1.1 (2003), you would need to create DataGridColumnStyle and set the Width to 0.  In order to hide a DataGridView column for 2.0 (2005), it is significantly easier.

Bob
Avatar of kdunnett
kdunnett

ASKER

How do you loop through a datagrid in 1.1?  its not like a datatable where u can use a datarow...

ideas?
What is the DataGrid bound to?

Bob
A dataset, which is a sql statement.

i'm thinking of doing something like the below... just call it for every column that is in the dataset, and handle bringing back the columns via sql:

            private void CreateBoundColumn(string columnName, string columnTitle, bool visable)
            {                        
                  BoundColumn bc = new BoundColumn();
                  bc.DataField = columnName;
                  bc.HeaderText = columnTitle;
                  if(!visable)
                        bc.Visible = false;
                              
                  this.dg.Columns.Add(bc);                              
            }
If you have a DataSet, then loop through the DataTable, and check the values in a particular column to see if they are all blank.

Bob
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks...  that's its.

I knew what it was...  just up for too long and getting forgetfull.