Wrox Programmer Forums
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 7th, 2005, 04:18 PM
Registered User
Join Date: Feb 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamically adding DataGrid rows

Hi all,

Got a problem I can't seam to figure out.

I have a form that contains 2 textboxes and 2 datagrids.

The textboxes hold a date that can be selected by clicking a button. the button opens a popup date selector that populates the forms date box.

The datgrids also have a button attached to them that brings up a popup where the user can select a value that is then dynamically inserted into the datagrid.

The insertions are working as expected. I click the button, the popup comes up, I set the values, click ok in the popup, the data is added as a new row in the datagrid. The unwanted behaviour is this. If I add my row to the datagrid through the popup, then select any of the buttons the form does a postback and my datagrid reverts to it's viewstate thereby erasing the row I just dynamically entered.

Is there a work around to this? For instance, is there a way to dynamically add the row to the datagrids viewstate. What can I do to ensure that the rows I dynamically add to the datagrids remain after a postback of the page?

Any help is appreciated.

Old February 7th, 2005, 04:46 PM
Friend of Wrox
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post

you can't add a row to a datagrid in this manner and its reason is exactly the same you mentioned,you must add a row to the datasource for your grid then re-bind the grid.also check out this thread.

software engineering student.
Looking for a good job for summer 2005.
Old February 8th, 2005, 04:50 PM
Registered User
Join Date: Feb 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

Hey all,

I fixed this issue and thought I would post the fix for those that may need it. As my previous post stated the issue I was having was my form was posting back every time I clicked the button to enter a date. Thereby, erasing all of the changes I had made to the datagrid.

What I did was replace the asp button that opened my datepicker and entry form for the datagrid with an html button. I set the html buttons onclick even to a custom method that opened the correct form. When the user completed all of the changes to both the date text box and the datagrid they click a save button that does a post back where all of the values are saved to the database.

The routine for updating the database was a little tricky because I had to pick out the rows in the table that had actually been changed. It's important to note that when I was looking for changes to the datagrid I did not use the datagrid itself. Instead I used the actual table on the form. Each time I added a new row I added that row with a different background color so the user could easily pick up that it was a new row. When I filtered for new rows from the table for entry into the database I just looked for the rows that had the different background color. I did all of this filtering client side.

Theres two keys here; one is using an html button instead of an asp button because the html button doesn't explicitly do a post back. The other is to work with the form table client side and not the datagrid server side.

Old November 22nd, 2006, 04:33 AM
Registered User
Join Date: Nov 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts


I am looking for code to add rows dynamically to the datagrid at client side. Can you post what code/script you used to add rows to the table control. Appreciate your help!

email : reachali@yahoo.com

Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding A Databound Column Dynamically in DataGrid Theone84 ASP.NET 2.0 Professional 0 July 30th, 2008 11:26 PM
Dynamically adding Colums to datagrid ? vivekshah C# 0 April 15th, 2006 02:54 AM
Adding customised rows to datagrid danielg C# 2 April 11th, 2005 05:35 PM
Adding new rows to Datagrid MixedCode General .NET 1 April 10th, 2005 05:00 PM
Dynamically adding a row to a DataGrid vwindrow ASP.NET 1.x and 2.0 Application Design 2 August 20th, 2003 03:10 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.