Link to home
Start Free TrialLog in
Avatar of BrianMc1958
BrianMc1958

asked on

LAZY PERSON: Code to hide null columns in table?

Dear Experts,

I am a lazy person who could figure this out myself eventually, but...  

Could anyone tell me how to hide all columns in a (datagrid) table in which all values are null?  

Thanks!
--BrianMc1958
Avatar of PoeticAudio
PoeticAudio

null columns? or do you mean null cells?

You can use a tablestyle and then set the nulltext for that column to a blank string.
Avatar of BrianMc1958

ASKER

Sorry.  I meant all null cells.  I need to have the entire column hidden in that case, as I have a few tables with just a few rows but lots of empty columns.  (All the values in the columns are empty.)
I did this within Excel once, and had to loop through all the rows.  That's practical here because there are few rows (but many, many columns...)
Is this Winforms or APS.NET?
Winforms.  Sorry to take so long.  Back is out.  Working (painfully) from home...
ASKER CERTIFIED SOLUTION
Avatar of anyoneis
anyoneis
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
Dear anyoneis,

Again, sorry to ignore you for so long.  Back has been very out.  Thanks so much for the code.  I'll give it a try!

--BrianMc1958
Brian, you probably saw this, but I left out an optimization in the inner loop that would be important if the number of rows gets larger:

                for (int j = 0; j < ta.Rows.Count; j++)
                {
                    if (ta.Rows[j][i] != System.DBNull.Value)
                    {
                        dataGridView1.Columns[i].Visible = true;
                        break;
                    }
                }

It would also be interesting to compare performance against this, which works fine also:

            for (int i = 0; i < ta.Columns.Count; i++)
            {
                string filter = ta.Columns[i].ColumnName + " IS NOT NULL";
                if (ta.Select(filter).Length > 0)
                {
                    dataGridView1.Columns[i].Visible = true;
                }
            }

Ciao!
David