?
Solved

LAZY PERSON: Code to hide null columns in table?

Posted on 2006-05-19
8
Medium Priority
?
249 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:BrianMc1958
  • 4
  • 3
8 Comments
 
LVL 6

Expert Comment

by:PoeticAudio
ID: 16721439
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.
0
 

Author Comment

by:BrianMc1958
ID: 16721495
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.)
0
 

Author Comment

by:BrianMc1958
ID: 16721498
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...)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 11

Expert Comment

by:anyoneis
ID: 16725127
Is this Winforms or APS.NET?
0
 

Author Comment

by:BrianMc1958
ID: 16738803
Winforms.  Sorry to take so long.  Back is out.  Working (painfully) from home...
0
 
LVL 11

Accepted Solution

by:
anyoneis earned 1000 total points
ID: 16739809
I give up. Looping thorugh the rows AND columns seems the only way to go.

As in:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NullGridColumnsHidden
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            DataTable ta = new DataTable("Table1");
            ta.Columns.Add("Col1", System.Type.GetType("System.Int32"));
            ta.Columns.Add("Col2", System.Type.GetType("System.Int32"));
            ta.Columns.Add("Col3", System.Type.GetType("System.Int32"));
            ta.Columns.Add("Col4", System.Type.GetType("System.Int32"));
            ta.Columns.Add("Col5", System.Type.GetType("System.Int32"));
            ta.Columns.Add("Col6", System.Type.GetType("System.Int32"));
            ta.Columns.Add("Col7", System.Type.GetType("System.Int32"));
            ta.Columns.Add("Col8", System.Type.GetType("System.Int32"));
            ta.Columns.Add("Col9", System.Type.GetType("System.Int32"));
            ta.Columns.Add("Col10", System.Type.GetType("System.Int32"));

            System.DBNull dbn = System.DBNull.Value;
            int ONE = 1;

            ta.Rows.Add(ONE, dbn, dbn, dbn, dbn, dbn, dbn, dbn, dbn, dbn);
            ta.Rows.Add(ONE, dbn, dbn, dbn, dbn, dbn, dbn, dbn, dbn, dbn);
            ta.Rows.Add(ONE, dbn, dbn, dbn, dbn, dbn, dbn, dbn, dbn, dbn);
            ta.Rows.Add(ONE, dbn, dbn, dbn, dbn, dbn, ONE, dbn, ONE, dbn);

            ds.Tables.Add(ta);
            BindingSource bs = new BindingSource(ds, "Table1");
            dataGridView1.DataSource = bs;

            for (int i = 0; i < ta.Columns.Count; i++)
            {
                dataGridView1.Columns[i].Visible = false;
            }


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

AnyoneElse?

anyoneis
0
 

Author Comment

by:BrianMc1958
ID: 16769374
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
0
 
LVL 11

Expert Comment

by:anyoneis
ID: 16770720
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
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question