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

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
0
BrianMc1958
Asked:
BrianMc1958
  • 4
  • 3
1 Solution
 
PoeticAudioCommented:
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
 
BrianMc1958Author Commented:
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
 
BrianMc1958Author Commented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
anyoneisCommented:
Is this Winforms or APS.NET?
0
 
BrianMc1958Author Commented:
Winforms.  Sorry to take so long.  Back is out.  Working (painfully) from home...
0
 
anyoneisCommented:
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
 
BrianMc1958Author Commented:
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
 
anyoneisCommented:
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
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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