Click here to Skip to main content
11,791,059 members (47,010 online)
Click here to Skip to main content

how to create columns dynamically in a grid view

, 17 Mar 2006 451.6K 4.3K 66
Rate this:
Please Sign up or sign in to vote.
This article describes about how to create columns dynamically in a grid view.
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

Sample Image - dynamic_Columns_in_Grid.jpg


This article describes about how to create columns dynamically in a grid view.
Many times we have the requirement where we have to create columns dynamically.
This article describes you about the dynamic loading of data using the DataTable as the datasource.

Details of the Grid

Let’s have a look at the code to understand better.


Create a gridview in the page,

  1. Drag and drop the GridView on to the page


  1. Manually type GridView definition in the page.


<table border="0" cellpadding="0" cellspacing="0">



                    <strong>Dynamic Grid</strong></td>




                    <asp:GridView ID="GrdDynamic" runat="server" AutoGenerateColumns="False">









With this we are done with creating a GridView in the page. Let’s move on to the code- beside to understand the background history of the page.


public partial class _Default : System.Web.UI.Page


    #region constants

    const string NAME = "NAME";

    const string ID = "ID";



    protected void Page_Load(object sender, EventArgs e)





    private void loadDynamicGrid()


        #region Code for preparing the DataTable


        //Create an instance of DataTable

        DataTable dt = new DataTable();


        //Create an ID column for adding to the Datatable

        DataColumn dcol = new DataColumn(ID ,typeof(System.Int32));

        dcol.AutoIncrement = true;



        //Create an ID column for adding to the Datatable

        dcol = new DataColumn(NAME, typeof(System.String));



        //Now add data for dynamic columns

        //As the first column is auto-increment, we do not have to add any thing.

        //Let's add some data to the second column.

        for (int nIndex = 0; nIndex < 10; nIndex++)


            //Create a new row

            DataRow drow = dt.NewRow();


            //Initialize the row data.

            drow[NAME] = "Row-" + Convert.ToString((nIndex + 1));


            //Add the row to the datatable.





        //Iterate through the columns of the datatable to set the data bound field dynamically.

        foreach (DataColumn col in dt.Columns)


            //Declare the bound field and allocate memory for the bound field.

            BoundField bfield = new BoundField();


            //Initalize the DataField value.

            bfield.DataField = col.ColumnName;


            //Initialize the HeaderText field value.

            bfield.HeaderText = col.ColumnName;


            //Add the newly created bound field to the GridView.




        //Initialize the DataSource

        GrdDynamic.DataSource = dt;


        //Bind the datatable with the GridView.




Let’s start dissecting right from the top,


After the page declaration, I have created 2 constants which represent the fields in the outputted grid. After that I am calling the loadDynamicGrid() method which will actually work for you. In this method,

  1. Creating a DataTable which will hold the table definition and data for the GridView. This table is used as a DataSource for the GridView
  2. Once the DataTable is created, I am creating 2 columns, ID & Name of type integer (ID) and string (Name) and adding them to the DataTable.
  3. Since the table structure is ready, I am generating some sample data by iterating through a loop for displaying in the GridView.
  4. Now I am iterating through the Columns of the DataTable to create the dynamic columns in the Gridview.
  5. The logic behind creating dynamic column starts by creating a BoundField instance.
  6. Once the BoundField is created, I am initializing the DataField and HeaderText with the column name of the DataTable.
  7. After the BoundField’s basic necessary properties are defined. I am adding the newly created bound field to the GridView control
  8. Once the creation of the dynamic columns is completed, I am assigning the DataSource of the GridView and calling the DataBind method to load the GridView with data dynamically.



That’s all your dynamic GridView is ready. I hope this information would be helpful.


In my next article, I will describe about how to create TemplateColumn dynamically. By now, you must have understand how easy is creating BoundColumn. To create any other type of columns like asp:ButtonField, asp:CheckBoxField, asp:CommandField, asp:HyperLinkField, asp:ImageField the logic & strategy is same, except the template column.

Enjoy programming.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Devakumar Sai Chinthala
Founder &
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Mittu Machhi27-Feb-13 19:16
memberMittu Machhi27-Feb-13 19:16 
GeneralMy vote of 5 Pin
AshishChaudha26-Feb-13 17:28
memberAshishChaudha26-Feb-13 17:28 
GeneralMy vote of 5 Pin
Mitesh Machhi5-Feb-13 20:10
memberMitesh Machhi5-Feb-13 20:10 
QuestionAdding a text box for capturing answers Pin
klca28-Dec-12 6:18
memberklca28-Dec-12 6:18 
QuestionKinda worthless Pin
Member 99049023-May-12 10:43
memberMember 99049023-May-12 10:43 
GeneralMy vote of 5 Pin
sravani.v20-May-12 20:11
membersravani.v20-May-12 20:11 
QuestionI understand how to dynamically add but how do you preserve values of control inside gridview Pin
Jephunneh Malazarte14-Feb-12 23:42
memberJephunneh Malazarte14-Feb-12 23:42 
GeneralMy vote of 1 Pin
navjyotjss10-Nov-11 22:52
membernavjyotjss10-Nov-11 22:52 
GeneralWorked a treat with an AccordionPane exercise Pin
IPI Paul12-May-11 3:34
memberIPI Paul12-May-11 3:34 
GeneralMy vote of 4 Pin
hasan.rounak13-Feb-11 22:06
memberhasan.rounak13-Feb-11 22:06 
GeneralMy vote of 4 Pin
kiranpatel17-Jan-11 1:26
memberkiranpatel17-Jan-11 1:26 
QuestionDynamic create a Gridview. Pin
Member 303777124-Aug-09 23:53
memberMember 303777124-Aug-09 23:53 
Generalclick event for the Imagebutton Pin
vijay r22-Jul-09 11:50
membervijay r22-Jul-09 11:50 
Questionhow to get the data that is alrady enterd in grid? Pin
kumar Umesh23-Dec-08 0:38
memberkumar Umesh23-Dec-08 0:38 
Questionhow to make the dynamic grid view editable Pin
Member 423285720-Oct-08 21:08
memberMember 423285720-Oct-08 21:08 
AnswerRe: how to make the dynamic grid view editable Pin
Shail Mishra21-Feb-14 23:53
memberShail Mishra21-Feb-14 23:53 
GeneralThanks Pin
Nishant Rana (Nishu)21-Sep-07 2:02
memberNishant Rana (Nishu)21-Sep-07 2:02 
GeneralNeeds Paging and Sorting Pin
JasonDWilson4-Apr-07 5:00
memberJasonDWilson4-Apr-07 5:00 
Questionhyperlink column Pin
gilavaa27-Aug-06 22:46
membergilavaa27-Aug-06 22:46 
Questiongridview header Pin
o5ama14-Aug-06 18:25
membero5ama14-Aug-06 18:25 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1509028.1 | Last Updated 17 Mar 2006
Article Copyright 2006 by Devakumar Sai Chinthala
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid