• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

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
0
kdunnett
Asked:
kdunnett
  • 4
  • 3
1 Solution
 
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
 
Bob LearnedCommented:
What is the DataGrid bound to?

Bob
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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
 
kdunnettAuthor Commented:
Thanks...  that's its.

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now