Click here to Skip to main content
15,881,172 members
Articles / Web Development / ASP.NET

Dynamic Gridview Columns

Rate me:
Please Sign up or sign in to vote.
3.21/5 (9 votes)
7 Apr 2008CPOL 74K   21   15
Very simple article to show how to add columns to a GridView at runtime.

Introduction

Sometimes you need to add columns to your GridView dynamically. In this article, I will show you how to do that in very simple steps.

Background

This code will show only how to create columns in the GridView at runtime, I will not show how to get the data source; you should already know how to do that.

Using the code

Let's say you have a query and you want to show the results in a GridView. For example:

SQL
SELECT UName,UAddress,Uphone From MyTable

In the GridView, if AutoGenerateColumns set to True, you will get the column names the same as the column names in the query.

First you have to set AutoGenerateColumns to False.

VB
GridView1.AutoGenerateColumns = False 

And then create the bound field:

VB
Dim Field As New BoundField

Then, inside it, select the data field and the header text:

VB
Field.DataField = "UName"
Field.HeaderText = "User Name"

Now, create a DataControlField and assign it to the bound field.

VB
Dim Col As DataControlField = Field

Then, add it to the GridView:

VB
GridView1.Columns.Add(Col)

The new column is added. Now, you want to add the new column and you have to make the new resource as follows:

VB
Field = New BoundField

Repeat the previous steps. Putting it all together:

VB
GridView1.AutoGenerateColumns = False
Dim Field As New BoundField
Field.DataField = "UName"
Field.HeaderText = "User Name"
Dim Col As DataControlField = Field
GridView1.Columns.Add(Col)

Field = New BoundField
Field.DataField = "UAddress"
Field.HeaderText = "User Address"
Col = Field
GridView1.Columns.Add(Col)

Field = New BoundField
Field.DataField = "UPhone"
Field.HeaderText = "User Phone Number"
Col = Field

GridView1.Columns.Add(Col)
GridView1.DataBind()

I hope this article helps.

License

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


Written By
Team Leader
Bahrain Bahrain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Dineshshp18-Jan-13 22:17
professionalDineshshp18-Jan-13 22:17 
Mast Code h Yaar...
GeneralMy vote of 2 Pin
Aditya Dabholkar29-Aug-12 1:25
Aditya Dabholkar29-Aug-12 1:25 
GeneralGood one Pin
aswathy.s.886-Jun-11 20:17
aswathy.s.886-Jun-11 20:17 
GeneralDatagrid columns Pin
mikedpettis5-Aug-10 15:22
mikedpettis5-Aug-10 15:22 
GeneralButtonField Column Pin
ckuroda2001@yahoo.com13-May-09 3:46
ckuroda2001@yahoo.com13-May-09 3:46 
GeneralRe: ButtonField Column Pin
ahmed-itani13-May-09 21:02
professionalahmed-itani13-May-09 21:02 
GeneralGreat Stuff Pin
halo222217-Nov-08 19:48
halo222217-Nov-08 19:48 
Generalproblem when pageindex is using Pin
udayakumarziv17-Nov-08 1:25
udayakumarziv17-Nov-08 1:25 
GeneralRe: problem when pageindex is using Pin
ahmed-itani19-Nov-08 0:41
professionalahmed-itani19-Nov-08 0:41 
QuestionAdd textBox into DataGridView Columns at runtime Pin
Biju VA20-May-08 19:48
professionalBiju VA20-May-08 19:48 
AnswerRe: Add textBox into DataGridView Columns at runtime Pin
ahmed-itani21-May-08 21:48
professionalahmed-itani21-May-08 21:48 
GeneralRe: Add textBox into DataGridView Columns at runtime Pin
Member 371362313-Aug-08 19:01
Member 371362313-Aug-08 19:01 
GeneralRe: Add textBox into DataGridView Columns at runtime Pin
ahmed-itani14-Aug-08 3:44
professionalahmed-itani14-Aug-08 3:44 
GeneralRe: Add textBox into DataGridView Columns at runtime Pin
Member 371362314-Aug-08 5:51
Member 371362314-Aug-08 5:51 
QuestionRe: Add textBox into DataGridView Columns at runtime Pin
arulb2w25-Oct-14 3:34
arulb2w25-Oct-14 3:34 

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.