Click here to Skip to main content
11,713,482 members (84,195 online)
Click here to Skip to main content

Tagged as

Hide DataGridView columns with no data in any row

, 8 May 2011 CPOL 30.4K 7
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;
                if (notAvailable)
                    grdView.Columns[clm.Index].Visible = false;
            return grdView;

Call RemoveEmptyColumns() in your datagridview:


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


About the Author

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

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

Skill Set

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

You may also be interested in...

Comments and Discussions

QuestionFor web, use this code: Pin
Ezz Khayyat18-Jan-15 23:34
memberEzz Khayyat18-Jan-15 23:34 
QuestionGOOD Pin
Saunak Saha14-Oct-14 1:43
memberSaunak Saha14-Oct-14 1:43 
QuestionCrash Fix Pin
lsvmky10-Apr-13 9:01
memberlsvmky10-Apr-13 9:01 
General>>>> .ToString() != string.Empty Hell, guys, what's year no... Pin
Member 30824874-May-11 5:17
memberMember 30824874-May-11 5:17 
GeneralEdit - modified title (original title in description) Pin
Indivara4-May-11 4:23
subeditorIndivara4-May-11 4:23 
Generalhi neil.hall,philm_be Youre right,I've some problem in my co... Pin
ambarishtv3-May-11 21:45
memberambarishtv3-May-11 21:45 
Generalarticle looks good! Pin
ThatsAlok 2-May-11 16:26
member ThatsAlok 2-May-11 16:26 
GeneralRe: article looks good! Pin
ambarishtv2-May-11 20:22
memberambarishtv2-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 | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 8 May 2011
Article Copyright 2011 by ambarishtv
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid