Ken Robertson's Blog

Ramblings of a .NET developer

DataGrid Help: Dynamically columned DataGrid and WebControls?

Hopefully someone can offer some insight into this... I've been starting to pull my hair out, and I am too young to go bald.

On this one page, I dynamically generate a couple of datagrids all though code (nothing in the webpage itself).  It is for a survey, and depending on how many question types are in the system, it has to generate a datagrid for each, and then it needs columns for each of the responses (Yes/No,  Excellent/Very Good/Good/Poor/Horrible).  So I can't use a static datagrid, since I don't know how many grids I need, and the number of columns will vary, so no way to use a <asp:RadioButton> for the responses.

I've got the tables to all show up.  I create the datagrid, create a datatable and add all of the columns and then the rows.  If I put the plain HTML for the radio button into the table, the radio shows up, but I cannot get its values when I submit it.  Not even though Request[”question1”].  If I create a RadioButton object and set the column to it, nothing shows up.  The column just disappears.  I've tried declaring an array of RadioButtons within the scope of the whole page, and still doesn't work.

Anyone have any ideas?  Some better way I could do it?  If I declare the DataGrid within the scope of the whole class will it work?  If it helps, you can check out my code.  Please!  Before I go bald!

Posted: Feb 28 2004, 08:55 AM by qgyen | with 4 comment(s)
Filed under: ,

Comments

John Mason said:

Ken, had the same problem and it can be done...as soon as I get back to my development server I'll post the code. What I would tell you though is that the standard datagrid is a pain in the ...
We have used a number of super grids that work with less code, are supported with examples...saves the hair. You probably know but you can find a number of grids on http://www.asp.net

For the time saved, the cost for most of these is reasonable.

John
# February 28, 2004 12:42 PM

Ramon Smits said:

Seems to me you are building your dynamic grid at the wrong time. Your grid has to be build up before ASP.NET will check your page for changes. Build the grid in the OnInit method and try to read the values in your OnLoad and everything will be fine. You will have to do this *every* time the page or control is loaded.

One thing that is quite difficult to maintain is the state of your page. You will have to know the state to build the correct grid. You could use query parameters or the viewstate to do this.

You don't supply any code and nowhere you mention where you are building your datagrid. Please supply more info if I did not understand your problem 100%.
# February 28, 2004 4:53 PM

Sumeet said:

I have a very similar problem in an interactive questionniare using datagrids generated at page load time depending upon the number of questions and answers. Still haven't worked out how to preserve the state of the check boxes in one of the grids columns which contain the user's response to the questions.
After sleeping on the problem for several weeks - I have decided to try and use XML either cached/written do disk to save the data from the check box column for each user session. During the ItemDataBound event of the datagrid check to whether a recent XML exists for that particular session & set of questions and rebuild the state of the checkbox column from this.
An overkill perhaps, but I am too fed up to come up with any solution.
# February 28, 2004 7:53 PM

TrackBack said:

# February 28, 2004 11:07 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)