Dynamically adding column to GridView kills existing template columns

I have a gridview with two template columns that were added at design time.  Each template contains an Item template which just displays the value and an Edit Item template that provides a drop down list during editing.

 These columns work fine.

 I would like to add a simple display column that is added to (or removed from) the grid when a user checks (or unchecks) a checkbox.  I've done this by adding the following code to my page_load rountine:

if (chkAddFieldOffice.Checked)//


BoundField NewField = new BoundField();

NewField.DataField = "FIELD_OFFICE";

NewField.SortExpression = "FIELD_OFFICE";

NewField.HeaderText = "FIELD OFFICE";

NewField.ShowHeader = false;

NewField.ReadOnly = true;

NewField.Visible = true;

grdWellNotes.Columns.Insert(0, NewField);


 The new column is added OK, and everything looks good until you refresh the screen again, then the template fields are blanked out.

 Any ideas?

Thanks, Mike Williams

Chesapeake Energy.


6/13/2008 6:10:33 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

1 Replies

Similar Articles

[PageSpeed] 5

Hi zmlw3,

It is a common mistake. You have to create column after every postback action, because these columns are created dynamcally which means they need to rebuild and rebind after postback. I suggest you can create them in a function, so everytime postback you just call it again.


Qin Dian Tang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
6/18/2008 6:34:29 AM

Similar Artilces:

remove a gridview column dynamically, removes controls in template columns
I have a interface with a treeview and a gridview. The gridview has two templatefields that handles insert/update/deletes. The gridview is handling editing of different tables so I create the other columns in the gridview dynamically as boundfields. The user selects the table to edit in the treeview. The problem I am having happens when the user selects a table to edit the second time. The code that executes removes all bound fields, (dynamically added when user selected the first table in the treeview), but not the two template fields, and then adds boundfields specifi...

Gridview with template columns added dynamically
Hi, I'm building a gridview and adding template columns dynamically (at runtime). They contain textboxes filled with data from a database. The gridview shows the data the first time it loads, but whenever the page postbacks, it loses all the data (and the textboxes ), although it keeps the columns added dynamically. Does anybody knows how to set on the state of the controls added to the gridview or how can I keep the data and get the changes made in the textboxes when the page postbacks. Thanks. Hi Matellor  ,Make sure you add these textboxes in page load event each postback.&...

Dynamically adding a column causes loss of templates in static columns
I have a DataGrid where the first column in the Grid is a template field. The values in that template field are set dynamically at run time. I recently added functionality that adds and removes some column dynamically based on criteria. Now, after I add a column dynamically, the next time I load the page the template for the template field is null, this is even before deleting any columns. If I use a watch at run time during the binding of the grid then if I loom at grid.Columns.NonPublicMembers.base._collectionItems.[0].ItemTemplate then I can see that there is an Item Template on the f...

How to check Column exist before adding to dynamic Gridview
I have a gridview that is based on a crosstab SP. The first 10 columns and the last (total) are fixed, but after that I could get 1 - 9 additional dynamc columns. Based on how many positions are involved determins how many extra dynamic columns to display. In this example, there are 3 postion columns. Task ID Disc code Act code Sub code Micro code Discipline Activity Subcode Microcode Qty Assc Engineer Engineer Senior Engineer Total Hours Edit 19245 9 1 4 0 Automation Definition Functional Requirements Specifications Functional Requirements Specificat...

Dynamically create controls in GridView template column
Hi,this is my first post so please be nice :)I have a situation where I'm loading GridView from XML data source and I need to dynamically create a TABLE control inside grids template column.One of table's columns has an editable TextBox, so there could be unknown number of rows in a gridview and unknown number of rows in each table.At the moment I'm creating those tables inside  GridView_RowDataBound event and all works fine.Problem is that when user clicks in "Update" button table controls are gone because they are dynamically created.My question is how can I crea...

Issue with Template Columns & Bound Columns while updating the data in a GridView control
Hi, I'm facing the below problem (In general, ObjectDataSource is driving me crazy ! ) I have a simple gridview control and an ObjectDataSource is used to populate the grid. I'm trying to update a row and the corresponding tableadapter methods & stored procedure are in place. Each row has 2 checkboxes and 2 textboxes in edit mode. When the change the values in edit mode and hit update, the checkbox values changed are saved into the Database properly, whereas the textbox values are not being saved. If the textbox column is a bound column then the column is updated with <NULL> in...

Adding ImageButton Column Dynamically to Dynamic GridView
I am binding a dataset dynamically to a GridView. To this I am adding one more column of image button type after the binding is done. I am facing the problem of adding "onClick" event to the button added. Using the  RowCommand event of the grid view I am trying to achieve the onClick eventBut the above method is not working when I navigate to a particular page and then come back.Could someone tell me where is it that I am going wrong. gvSummary is my gridview. DataSet dsMainSummary = new DataSet(); dsMainSummary = ReturnMainSummary(); gvSummary.DataSour...

Bound columns and template columns in Gridview
Hello Everyone, In my gridview, I changed some of the bound columns to template columns, I noticed that the template column is not recognized inside the RowDataBound event method. The reason I changed the bound columns to template columns so that I can add the total column in the footer. I guess I cannot do that with bound columns. If I do e.Row.Cells[5].Text then  5th column is not recognixed inside the Rowdatabound event, but If i do e.Row.Cells[4].Text then  it is recognized. Please let me know what am I doing wrong. <asp:GridView ID="dgAgentActivity" runat="server" AutoGen...

trouble creating a reusable GridView with dynamic Template command Columns VB.NET
I've created a single GridView control that I want to reuse.  My goal is to bind the initial dataset with a programmatically created command column, pass the arguments to a universal RowCommand event handler and dynamically change the DataSource, commandArgument, commandName, and Databind again. I haven't had any success in implementing this idea.  I've tried creating separate template class and Import it, but then I'm unable to assign the properties to the Command column. I just need someone to point me in the right direction.  I've found hundred...

Dropdown event handler not firing in Gridview dynamically added template column header
 The auto postback is working but I try to redirect to a page in the event and that's not happening.  I add the column in the Page Init event.  I'm really stuck on this, I've looked and looked and can't find the answer. public void InstantiateIn(System.Web.UI.Control container)        {.......             ddTaskCode.Width = Unit.Pixel(150);            ddTaskCode.ID = "ddTaskCode" + columnName;  ...

How to determine the row number when clicking a delete button I added to a template column from a GridView control???
Hello all,I am working using the codebehinde the page with VB.NET 2005 (web application).  I have a gridview where I added a template column, inside that templated column I added a delete button.  I would like to know how could I determine which row number corresponds to the clicked button so I can delete the current record.This is what I have: Protected Sub LinkButtonDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Try Dim cmd As SqlCommand Dim objConnection As SqlConnection objConnection = New SqlConnecti...

Adding column programatically to GridView is re-adding a repeate column on every Bind
In my code, I'm adding a column programatically to my GridView.  My code keeps adding the CT column on every bind.  This is in a Bind Function I created that I'm using on different events throughout my page.  Should I check for it's existance first before adding or is there a better way to handle this?  I end up with a new column for this every time I bind.  I want to know if there is a way to resolve this without just throwing in a dirty If statement.  Is there anoter place in my code or something else I should be doing to prevent every bind from readding a col...

Dynamically Adding a Template Column
Hello Everyone and thanks for your help in advance. I am writing an application that requires datagrids to be added dynamically to a page. However, I need to set up one of the columns as a template column and can't seem to get that part of it correct. Do I need to set up an outside template (if so, how) or can it be done within the code? Any help would be greatly appreciated. Thanks. Add ItemTemplate dynamically Check if this is what u want Sushila Bowalekar PatelVisual ASP/ASP.NET MVPhttp://weblogs.asp.net/sushilasb...

GridView Dynamic Columns and Templates
I'm trying to utilize the GridView to provide editing data of several SQL tables on the fly.  Basically I've got a dropdownlist at the top of the screen that shows a user a list of available tables that they can edit.  When the user selects/changes a table from the list, the datasource that the gridview is linked to is changed to retrieve the appropriate columns for the table selected by the user.  (These tables have completely different structures.)  What I've described so far works fine.  Where it breaks down is when I want to edit a particular row and I want drop ...

Web resources about - Dynamically adding column to GridView kills existing template columns - asp.net.presentation-controls

iPhone 5 A6 chip to dynamically up-clock up to 1.3GHz (and downclock too)?
Interesting video from our friends at TLDToday: If this is not just a Geekbench app glitch, it could indicate why that A6 processor is so 2X ...

Apple's Lightning port dynamically assigns pins to allow for reversible use
... a closer look at how Apple's new Lightning connector works, and has come to the conclusion that the 8 pins on each side of the plug are dynamically ...

iPhone 5 Lightning port dynamically reassigns pins
... of Apple's Lightning USB cable shows that the pins on the plug aren't arranged symmetrically, suggesting that the Lightning port can dynamically ...

Urlbox Launches Screenshots-as-a-Service Platform to Dynamically Update Galleries
... changes. Launched yesterday, Urlbox allows anyone with a gallery displaying website screenshots to automate the process and keep them dynamically ...

Everything.me launcher dynamically adapts to what you’re doing right now
... phone according to what you’re doing. Perform a search for ‘football’ and your background will change to the NFL logo. A launcher that dynamically ...

A YouTube Experiment Resizes the Player Dynamically
There's a new YouTube experiment that changes the video player's size, depending on the browser window's size. When resizing Chrome's window, ...

Apple Granted “Dynamically Changing Cursor for User Interface” Patent, Originally Filed in 2003
... a utility patent that covers the use of the mouse cursor that changes according to the context of the task it is engaged in. Called a “Dynamically ...

Atmel's FPSLIC II Dynamically Reconfigurable SoC Supports "Silicon-Sharing" For Peripherals & Interfaces ...
Silicon-Sharing Fits 200K Gate Design into 40K Gate FPSLIC II's, Cuts Power Drain by 97%

Everything.me Launcher Wants to Dynamically Change Phones Based on Location, Interests, and Mental State ...
What if your phone’s launcher dynamically changed based upon interests, your location, or topics you had in mind at a specific moment? A new ...

Resources last updated: 12/24/2015 9:24:30 PM