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

Tip: Accessing AutoGenerated Columns in GridView

Rate me:
Please Sign up or sign in to vote.
4.92/5 (4 votes)
5 Aug 2016CPOL1 min read 9.7K   4  
A tip that demonstrates how to access GridView AutoGenerated Columns.

Introduction

I've seen many developers in the forums were asking stuff like “how do we set read-only for autogenerated columns in gridview?” or "how to access autogenerated bound columns in GridView?". Well as you may know, autogenerated columns are created dynamically on the fly and so we need to manually access each columns in the code before we can set their properties.

Using the Code

Here's a quick code snippet on setting the boundfield column for autogenerated columns in GridView to ReadOnly.

C#
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{  
            foreach (TableCell cell in e.Row.Cells)
             {  
                if (!string.IsNullOrEmpty(cell.Text) && cell.Text != " ") 
                {  
                    BoundField field = (BoundField)((DataControlFieldCell)cell).ContainingField;  
                    if (field.DataField == "ID")  
                        field.ReadOnly = true;  
                }  
            }  
}

GridView cells are composed of different DataControlFields and basically AutoGenerated Columns uses a BoundField for displaying the data. If you have noticed from the code above, we loop through the cells and cast them to a DataControlFieldCell type to get the ContainingField. We then cast the ContainingField to a BoundField type so that we can check the DataField used in a particular column.

You could also use the code above if you want to (for example) hide a specific column in your auto generated grid.

But Why Not Just Use This?

C#
GridView1.Columns[index].Visible = false;

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

Here's the quick snippet for hiding a specific column:

C#
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
        foreach (TableCell cell in e.Row.Cells)
        {
            BoundField field = (BoundField)((DataControlFieldCell)cell).ContainingField;
            if (field.DataField == "ColumnName")
            {
                field.Visible = false;
            }
        }
}

That's it! I hope someone finds this post useful. 

License

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


Written By
Architect
United States United States
A code monkey who loves to drink beer, play guitar and listen to music.

My Tech Blog: https://vmsdurano.com/
My Youtube Channel: https://www.youtube.com/channel/UCuabaYm8QH4b1MAclaRp-3Q

I currently work as a Solutions Architect and we build "cool things" to help people improve their health.

With over 14 years of professional experience working as a Sr. Software Engineer specializing mainly on Web and Mobile apps using Microsoft technologies. My exploration into programming began at the age of 15;Turbo PASCAL, C, C++, JAVA, VB6, Action Scripts and a variety of other equally obscure acronyms, mainly as a hobby. After several detours, I am here today on the VB.NET to C# channel. I have worked on Web Apps + Client-side technologies + Mobile Apps + Micro-services + REST APIs + Event Communication + Databases + Cloud + Containers , which go together like coffee crumble ice cream.

I have been awarded Microsoft MVP each year since 2009, awarded C# Corner MVP for 2015, 2016,2017 and 2018, CodeProject MVP, MVA, MVE, Microsoft Influencer, Dzone MVB, Microsoft ASP.NET Site Hall of Famer with All-Star level and a regular contributor at various technical community websites such as CSharpCorner, CodeProject, ASP.NET and TechNet.

Books written:
" Book: Understanding Game Application Development with Xamarin.Forms and ASP.NET
" Book (Technical Reviewer): ASP.NET Core and Angular 2
" EBook: Dockerizing ASP.NET Core and Blazor Applications on Mac
" EBook: ASP.NET MVC 5- A Beginner's Guide
" EBook: ASP.NET GridView Control Pocket Guide

Comments and Discussions

 
-- There are no messages in this forum --