Click here to Skip to main content
Click here to Skip to main content

Hide DataGridView columns with no data in any row

, 8 May 2011
Rate this:
Please Sign up or sign in to vote.
How to remove columns' visibility when their rows in the column are empty in DataGridView control
Here, I have developed a new feature in datagridview control with the help of an extension method.
 
Suppose you created a dynamically generated datagridview which contains 'n' number of columns per row. This method is to help to hide columns when all rows values for that column are empty.
 
Code using DataGridView extension method RemoveEmptyColumns() is below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace MyProject
{
    public static class ExtensionGridView
    {
        public static DataGridView RemoveEmptyColumns(this DataGridView grdView)
        {
            foreach (DataGridViewColumn clm in grdView.Columns)
            {
                bool notAvailable = true;
 
                foreach (DataGridViewRow row in grdView.Rows)
                {
                    if (! string.IsNullOrEmpty(row.Cells[clm.Index].Value.ToString()))
                    {
                        notAvailable = false;
                        break;
                    }                   
                }
                if (notAvailable)
                {
                    grdView.Columns[clm.Index].Visible = false;
                }
            }
 
            return grdView;
        }
    }
}
 
Call RemoveEmptyColumns() in your datagridview:
  DataGridView1.RemoveEmptyColumns();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

ambarishtv
Software Developer
India India





Hi I' Ambarish from India. I've three and half year of experience in application
development solutions.

Educational Qualification

MCA -  from KristuJyoti College of management and technology (MG
University)

B.Sc (Physics) - from NSS College, Changanacherry (MG University)

Skill Set

C#,winforms,asp.net,MVC 3, Java,Design Patterns,VB, Android,JQuery Mobile,SQL,MySqlUML.


Comments and Discussions

 
QuestionCrash Fix Pinmemberlsvmky10-Apr-13 9:01 
General>>>> .ToString() != string.Empty Hell, guys, what's year no... PinmemberMember 30824874-May-11 5:17 
GeneralEdit - modified title (original title in description) PinsubeditorIndivara4-May-11 4:23 
Generalhi neil.hall,philm_be Youre right,I've some problem in my co... Pinmemberambarishtv3-May-11 21:45 
Generalarticle looks good! Pinmember ThatsAlok 2-May-11 16:26 
GeneralRe: article looks good! Pinmemberambarishtv2-May-11 20:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 8 May 2011
Article Copyright 2011 by ambarishtv
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid