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

Tagged as

Hide DataGridView columns with no data in any row

, 8 May 2011 CPOL
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.

Comments and Discussions

QuestionFor web, use this code: PinmemberEzz Khayyat19-Jan-15 0:34 
QuestionGOOD PinmemberSaunak Saha14-Oct-14 2:43 
QuestionCrash Fix Pinmemberlsvmky10-Apr-13 10:01 
General>>>> .ToString() != string.Empty Hell, guys, what's year no... PinmemberMember 30824874-May-11 6:17 
GeneralEdit - modified title (original title in description) PinsubeditorIndivara4-May-11 5:23 
Generalhi neil.hall,philm_be Youre right,I've some problem in my co... Pinmemberambarishtv3-May-11 22:45 
Generalarticle looks good! Pinmember ThatsAlok 2-May-11 17:26 
GeneralRe: article looks good! Pinmemberambarishtv2-May-11 21: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
Web04 | 2.8.150123.1 | Last Updated 8 May 2011
Article Copyright 2011 by ambarishtv
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid