Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

I decided to write this example because this has been asked many times before at the forums.

As you may know, we cannot "directly" hide AutoGenerateColumns in our codes using the code below:

GridView1.Columns[index].Visible = false;

Why?

This is because auto generated columns are not added in the GridView columns collection.Using the code above will give you "index was out of range error".

In this example I will show the different ways on how to hide specific column in GridView with AutoGenerateColumns set to TRUE.

Option 1:  Using the Cells index

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

{

        //Just changed the index of cells based on your requirements

        e.Row.Cells[0].Visible = false;

}

 

Option 2: Looping through GridView Row Controls collections

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

{

        //Just changed the index of cells based on your requirements

        foreach (TableRow row in GridView1.Controls[0].Controls)

        {

            row.Cells[0].Visible = false;

        }

}

 

You can use the above options for hiding the columns if you are sure with the order of the columns in the Table. Please note that autogenerated columns will display all the columns from the DataSource, so you must be careful when using index for hiding the columns.

Option 3: Looping through GridView Cells

As you may know the GridView cells are composed of different DataControlFields and basically AutoGenerated fields uses a BoundField for displaying the data. In this case we can loop through the cells generated by the GridView and cast the cell to a DataControlFieldCell type to get the ContainingField then we can cast this ContainingField to a BoundField sothat we can check the DataField used in a particular AutoGenerated BoundField and Hide them using its visible property.

To make it more clearer then you can check this code block below:

rotected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

{

   //Just set the Column Name that you wish to hide based on your requirements

        foreach (TableCell cell in e.Row.Cells)

        {

            BoundField field = (BoundField)((DataControlFieldCell)cell).ContainingField;

            if (field.DataField == "ColumnName")

            {

                field.Visible = false;

            }

        }

}

 

As you can see, we check for the ColumnName first before hiding the column instead of using column indexing.

The ColumnName above indicates the field from your DataSource that you wan’t to hide. You can use this option for hiding the columns if you do not know the sequence of the columns from the DataSource.

That’s it! Hope you will find this example useful!

 

Technorati Tags: ,,,
Posted on Wednesday, June 24, 2009 8:14 PM ASP.NET , C# , Tips&Tricks , GridView | Back to top


Comments on this post: Ways on how to Hide AutoGenerateColumns in GridView.

# re: Ways on how to Hide AutoGenerateColumns in GridView.
Requesting Gravatar...
very good post
Left by javal on Aug 10, 2009 7:03 PM

# re: Ways on how to Hide AutoGenerateColumns in GridView.
Requesting Gravatar...
Thanks. This code helped me a lot.
Left by Murthy on Apr 14, 2010 12:35 PM

# re: Ways on how to Hide AutoGenerateColumns in GridView.
Requesting Gravatar...
Good post,keep up the good work.
Left by alaa9jo on May 18, 2010 8:44 PM

# re: Ways on how to Hide AutoGenerateColumns in GridView.
Requesting Gravatar...
ur code helped me lot.. thanks
Left by ramya on Jun 09, 2010 1:56 PM

# re: Ways on how to Hide AutoGenerateColumns in GridView.
Requesting Gravatar...
Thanks for the article.
For the record, in the last example, you can avoid an explicit cast by doing this directly:

foreach (DataControlFieldCell cell in e.Row.Cells)

Also, in my case, I also had to add:

if (!(cell.ContainingField is BoundField))
{
continue;
}

at the beginning of the foreach, because not all of my columns were BoundFields.
Left by Stifu on Jul 21, 2010 5:48 PM

# re: Ways on how to Hide AutoGenerateColumns in GridView.
Requesting Gravatar...
It´s more simple

protected void HideColumnsGridViewAutogenerate(GridView handle, GridViewRowEventArgs e, int ind)
{
handle.HeaderRow.Cells[ind].Visible = false;
e.Row.Cells[ind].Visible = false;
}
Left by Paulo Tolentino on Aug 23, 2010 9:16 PM

# re: Ways on how to Hide AutoGenerateColumns in GridView.
Requesting Gravatar...
I tried to copy this code and its working for me. I tried all the examples. Please let me know what I am missing. I am pasting my code in my gridview control code.
Left by padkavi on Jan 07, 2011 1:41 AM

# re: Ways on how to Hide AutoGenerateColumns in GridView.
Requesting Gravatar...
Saved my code! Tks a lot!
Left by Andre on Mar 19, 2011 2:12 AM

# re: Ways on how to Hide AutoGenerateColumns in GridView.
Requesting Gravatar...
If you're using .NET 4 there is a simpler option:
http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.autogeneratingcolumn.aspx
Left by Jon on Sep 05, 2011 6:12 PM

# re: Ways on how to Hide AutoGenerateColumns in GridView.
Requesting Gravatar...
Thanks .u save my lots of time
Left by sunayana on Nov 26, 2011 6:53 PM

# re: Ways on how to Hide AutoGenerateColumns in GridView.
Requesting Gravatar...
Thanks - worked a treat (just had to convert to VB!)
Left by Blujay on Sep 21, 2012 9:26 PM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net