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
kdunnettAsked:
Who is Participating?
 
Bob LearnedCommented:
Or, because I assumed that you were talking about WinForms, if this is an ASP.NET DataGrid then you can do this:

foreach (DataGridItem item in this.dataGrid1.Items)
{
}

Bob
0
 
Bob LearnedCommented:
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
0
 
kdunnettAuthor Commented:
How do you loop through a datagrid in 1.1?  its not like a datatable where u can use a datarow...

ideas?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Bob LearnedCommented:
What is the DataGrid bound to?

Bob
0
 
kdunnettAuthor Commented:
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);                              
            }
0
 
Bob LearnedCommented:
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
0
 
kdunnettAuthor Commented:
Thanks...  that's its.

I knew what it was...  just up for too long and getting forgetfull.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.