How to – Dynamically add rows and columns to Grid View

Hi to add rows and columns to Gridveiw dynamically what
we will do is that

1) Create a DataTable object to which than we will bind the GridView
DataTable dt=new DataTable();

2) IF you need two columns than create two DataColumn object
DataColumn dCol1=new DataColumn(FirstC, typeof(System.String)); // string FirstC=”column1″
DataColumn dCol2=new DataColumn(SecondC, typeof(System.String)); // string SecondC=”column2″

Add it to the table


3)Run the loop for as many rows you want to add.

// say you want to add two rows

for(int i=0;i<2;i++)
DataRow row1 = dt.NewRow();
row1 [FirstC] = “One”;
row1 [SecondC] =”Two”
dt.Rows.Add(row1 );


Now iterate through each datacolumn and create a BoundField foreach column

foreach (DataColumn col in dt.Columns)
BoundField bField = new BoundField
bField.DataField = col.ColumnName;
bField.HeaderText = col.ColumnName;
GridView1.DataSource = dt;
//Bind the datatable with the GridView.


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

47 thoughts on “How to – Dynamically add rows and columns to Grid View”

  1. Hello,

    Adding Cols & rows Dynamically in the GridView is good one.

    But here I m facing one problem.

    As I have DataTable dt with the column as follows

    dt[“Sales-1001”] = 85
    dt[“Expiry-1001”] = 90

    dt[“Sales-1002”] = 66
    dt[“Expiry-1002”] = 44

    Now I have bind the columns as u told above
    but here I want the Column.Header text is different.

    so I did :

    foreach (DataColumn dcol in dt.Columns)
    BoundField bField = new BoundField();
    bField.DataField = dcol.ColumnName;
    bField.HeaderText = dcol.ColumnName.ToString().Substring(0, dcol.ColumnName.LastIndexOf(‘-‘));

    GridView1.DataSource = dt;

    But is is giving me double Columns Attached to GridView.
    Which have header Sales,Expiry,Sales,Expiry,Sales-1001,Expiry-1001,Sales-1002,Expiry-1002 like this

    it is showing boud field name too in the header.

    what to do..Suggest



  2. Just Include the lign below befopre setting the datasource.

    GridView1.AutoGenerateColumns = false;

    While creating a dynamic gridview you might have left the autogeneratecolumns property to true, which actually renders all the columns of the of the source datable irrespective of any bound columns created during runtime.

    I hope it works for you.

    Happy Programming!
    HookLogic Inc


  3. string[] fields = new string[] { “Icon”, “Name”, “ToolTip”, “Document”};

    foreach (string field in fields)
    TemplateField customField = new TemplateField();
    customField.ShowHeader = false;
    customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, field, tempGrid.ID, this.Page);

    You have to implement your own GridViewTemplate class, send me a mail if you want mine.


  4. How to add a row to a gridview using javascript?
    consider that when page loaded gridview dont have any rows and it will not be displayed.


  5. hello … can u help me with a problem similar to this… i have a page in vb , it has a gridview . i want the gridview to generate a number of columns which is equal to a query of record count from a database…

    i mean gridviewcolumns= record count .


  6. i am confused with small application i want to transfer textbox,dropdownlist,fileupload values to gridview when clicking “transfer button” … beside that one colum (it could be templatefield with delete button or command field ‘delete’) will be added dynamically for each row .. how to do that??????


  7. I need my dynamically populated Grid View be sortable. I did all that as explaineed here, and I set AllowSortin attribute to true. But when I click column header I get an error: “The GridView ‘GridView1’ fired event Sorting which wasn’t handled”. Please help – thanks.


  8. Hi
    how to show report in two columns in a page e.g. question paper displayed like Q1,2,3,4,5 in one column and second column display next 5 question for one page


  9. I want to add a row of checkboxes beneath the Gridview header row. The user will check the boxes of columns they want to hide. There will be a button to reveal all the columns again. The checkbox function I can figure out, but do you have any ideas for adding that row of checkboxes?



  10. Hi there! This is my 1st comment here so I just wanted
    to give a quick shout out and tell you I really enjoy reading your posts.
    Can you recommend any other blogs/websites/forums that cover the same topics?
    Thanks a ton!


  11. I’m extremely inspired along with your writing talents and also with the layout in your weblog. Is that this a paid subject or did you modify it your self? Either way stay up the excellent quality writing, it’s uncommon to see a
    nice weblog like this one today..


  12. You can let them know what to expect and what will be expected of them at the
    dental visit. Most pharmacy technician employers require certifications from
    those who aspire to take on the responsibilities of
    this job. If you having a gap in your teeth problem, a Dentists
    In Glendale can easily fill the space between teeth, so you would never notice that you ever had space in teeth after treatment.


  13. A person necessarily help to make seriously posts I might state.
    That is the first time I frequented your web page and so far?
    I surprised with the analysis you made to make this particular publish extraordinary.

    Fantastic job!


  14. I’m not that much of a internet reader to be honest but your sites
    really nice, keep it up! I’ll go ahead and bookmark your website to come back in the future.
    Many thanks


  15. %first_paragraphBut how do you drain the battery when Windows wont let you do just that? Theyre not safe (you might forget to change them back), they may not be getting an accurate reading, and they quite possibly wont let you set the critical Newest ASUS N73SW Battery level to 0 percent. (If they did, it would crash Windows. Dont bother with the settings described above.

    Coming in great shades of amazing silver, sparkling green, soothing blue, and juicy orange, you can also choose austere gray, as well. Taking pictures with a phone doesnt always work out the best, especially when you want high quality photos, but with this camera, you can get pretty decent pictures by snapping away when wanting to cherish some really special memories. With its 12 MP sensor and Carl Zeiss optics, you will have an amazing camera in your hands, one that will transform your phone picture capturing experience.

    For example, Making an anonymous call and telling that the bomb has been fixed in a school, which is going to burst in few minutes. It may be true or false. Without any aim to cause pain to someone, telling something about him or her to others which actually cause pain. Another type of gossiping is dishing which is done unintentionally. Fears or Concerns is involved with rumors being told about any life threats. It actually brings embarrassement to him or her who is being told about. It involves with many lives and it leads to bedlam. Likewise Tsunami Threat, Hijacking are some of the rumors which involve with peoples lives and concerns.

    This will allow you to easily locate a particular location. There are also GPS navigation services with Google Maps, you’ll be able to streets, towns and cities. This will be especially useful if you are new to a particular area and need directions to a particular place. You can also increase the storage size by using the microSD slot. As such, you will be able to major events in your life to capture. The camera comes with image stabilization and auto focus so you will be able to take good quality pictures, even if you’re an amateur. The camera comes with a camera that helps you to record videos and take good videos. 512 MB RAM is greater than many of the personal computers. It saves you the embarrassment of having to ask for directions of total strangers. Remember that moments explained but experienced.

    The prices start around 250 (but you can check the prices on this site called website The N150 comes in different colours too, you can get it in black, blue, red, white and yellow and what’s more the cover is scratch resistant and finger print proof.

    As the whole day you are spending to watch your favorite games then it must be essential for you to have a pleasant, comfy and reliable seat on which you feel relax. The Rock n fold chair helps you to prevent the aches and pains that may cause of sitting whole day for your favorite game
    Rock N Fold Chair If you will watching the football game, or cricket throughout the day then you need to have a Rock n fold chair as that helps you to feel relax and comfortable while you are watching the games.

    it is usable, but I wouldnt choose it over other gadgets which i presently use. However, when you do not consider only a tiny display getting an impediment, it performs pretty properly in coordination using the ambient mild sensor. I just cannot get over the actuality that it is way as well small getting very helpful as something other than a cell phone and tracks player. pictures and fonts are completely readable below instant sunlight, and all through the dark, the display appears great.

    The Nokia N97 mini appears damn exceptional and packs many features, but I would advise other gadgets earlier to this one i have been attempting to take advantage of the Nokia N97 mini to create a thorough review, but I consistently ended up placing it straight down quickly. need to master what I think within of just one sentence? Its hard to such as the S60v5 once more pursuing utilizing a Maemo equipment such as the Nokia N900.

    The Samsung N120 looks great and performs well. However, if you hope to do something more complex than that, it is better to choose another model. Everyday tasks such as browsing and playing music or videos can be implemented without glitches. The portability is due to its durability, low weight and battery life improved. We still think it’s too high priced for the features it offers.

    that is not really a massive subject as a massive amount of people will possibly obtain their non-public theme anyway, but only a tiny innovation I really feel is worth mentioning. The back again include consists of a pretty amazing function which modifications theme in the cell phone to complement the color in the back again include that you just positioned near to the handset.

    Nonetheless most cameras use an inexpensive passive AF lens whilst the expensive cameras will have an energetic AF lens or at times expert cameras could have each. AF lens compromises of each passive and lively categories. AF lens enables us to appreciate an less difficult method of photography encounter which is not complicated and also focus on capturing the moment than attempting to concentrate.%


  16. I hɑve been surfing on-line more than 3 hours lately, ʏet I never discovered any attention-grabbing articⅼe likе yours.
    It’s pretty worth sufficient fоr me. Personally, if all web owners and bloggers made ɡood content as you
    did, the net shall be a lot more helpfuⅼ than ever before.


  17. Great post. I waѕ checқing continuously this blog and I am impressed!
    Very helpfսl info specially the last phase :
    ) I deal with sucɦ information much. I was seeking tҺis certain information for
    a very lengthy time. Thank you and best of luϲk.


  18. An оutstanding share! I haᴠe just forwardeԀ thiѕ ontօ a friend who has been conducting a ⅼittlе homework on this.
    And he in fact bⲟught me dinner simply becauѕe Ι stumbled upon it
    foг him… lol. So let me rеword this…. Thаnks for the meal!!
    But yeah, thanks for spending some time to discuss this subjeϲt һere on your web page.


  19. Do you mind if I quߋte a couple of your articles as long as I provide
    crᥱdit and soᥙrces back to your blog? My blog is in the еxact same area of interest
    as youгs and my users would certainlʏ benefit from some
    of the information you present here. Please let me know if this ok with you.
    Appreciate it!


  20. Set “allowpaging=true” and “pagesize=X” (change X to how many rows you want visible).

    In the above single line i want columns and not rows to visible ,how can i show columns . X for columns , currently it is fixed to 1 columns remain 1 while we can change row . i want row to be 1 and columns as X.

    Where can i do this in gridview ..?


Please share your thoughts

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

My Trial

It is my experience timeline.


Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog & Website of Paul Andrew

Technical Leadership Centred Around the Microsoft Data Platform

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Dynamics 365 Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform


Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions


Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM


Mail to for queries and suggestions

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM


Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

%d bloggers like this: