Click here to Skip to main content
15,849,163 members
Articles / Programming Languages / C#
Tip/Trick

Hide DataGridView columns with no data in any row

Rate me:
Please Sign up or sign in to vote.
4.20/5 (7 votes)
8 May 2011CPOL 57.6K   7   8
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:
C#
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)


Written By
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

 
QuestionFor web, use this code: Pin
Ezz Khayyat19-Jan-15 0:34
professionalEzz Khayyat19-Jan-15 0:34 
QuestionGOOD Pin
Saunak Saha14-Oct-14 2:43
professionalSaunak Saha14-Oct-14 2:43 
QuestionCrash Fix Pin
lsvmky10-Apr-13 10:01
lsvmky10-Apr-13 10:01 
General>>>> .ToString() != string.Empty Hell, guys, what's year no... Pin
Thornik4-May-11 6:17
Thornik4-May-11 6:17 
GeneralEdit - modified title (original title in description) Pin
Indivara4-May-11 5:23
professionalIndivara4-May-11 5:23 
Generalhi neil.hall,philm_be Youre right,I've some problem in my co... Pin
ambarishtv3-May-11 22:45
ambarishtv3-May-11 22:45 
Generalarticle looks good! Pin
ThatsAlok2-May-11 17:26
ThatsAlok2-May-11 17:26 
GeneralRe: article looks good! Pin
ambarishtv2-May-11 21:22
ambarishtv2-May-11 21:22 
Thank you for your constructive feedback. Smile | :)
I will try my best to improve in presentation.
-ambarish-

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

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