Click here to Skip to main content
15,921,156 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
how can i hide empty or null value columns in gridview.
Posted

bind the non empty column data by processing data before binding. for example if you load the data to a DataTable from the sql server, you can remove empty column in the DataTable ( refer : Remove all columns with no data from DataTable[^]) and bind it as data source.
 
Share this answer
 
What you can do is check the columns for NULL or empty in RowDataBound() event of GridView and hide the columns accordingly. In the event method check first for DataRow like this and repeat for other columns

C#
if (e.Row.RowType == DataControlRowType.DataRow)
            {
               // To hide the first column
               string vToCheck = e.Row.Cells[0].ToString();
               if(string.IsNullorEmpty(vToCheck) )
                {
                gridView.Columns[0].Visible = false;     
                }

            }
 
Share this answer
 
v2
Please see:
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datacontrolfield.visible%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.web.ui.control.visible(v=vs.110).aspx[^].

As you can see, you can change visibility of any instance of DataControlField or any Control, including TableCell. Then look at the structure of the table. It has Rows and Controls properties, and the cells are the elements of rows collection. Scan all data in columns in questions, show or hide whatever you want depending on your condition.

—SA
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900