Changing column type of GridView with dynamic SQL (variable number of columns / autogeneratecolumns=true)

As you can guess from the title, I have a page that uses a GridView. AutoGenerateColumns is set to true and the SQLDataSource object is fed a dynamic SQL procedure that returns a variable number of columns depending on the value of a DropDownList (also kept in a global class variable). Now, these columns are all rendered as labels. But I need columns 4 and 8 to last-5 to be textboxes. That is, first 7 (except fourth) and last 5 remain labels, the others become textboxes.
How would I go about accomplishing this? Is there any event where I can set this just before the data is dumped? If so, can I also change other properties, like BackColor?
Also, how can I change the header properties separately?
LVL 18
CluskittAsked:
Who is Participating?
 
CodeCruiserCommented:
>But the columns aren't being created, despite the autogeneratecolumns property being set to true.

Actually, that(setting autogeneratecolumns=true) is the reason columns are not being created.

You can manually add controls to the Cells collection in RowDatabound event.
0
 
CodeCruiserCommented:
I think you can achieve this in RowDataBound event of the GridView

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx
0
 
CluskittAuthor Commented:
I think you can only change data at that stage. The ControlStyle property is already set. I can only change the properties of the cells in each row. The columns themselves are already set (also, the columns collection is empty).
I would be OK with creating all columns as textboxes. At databound I could just run through the ones I don't want changed and disable them. Disabled textboxes work just as well as labels.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CodeCruiserCommented:
Give that a try.
0
 
CluskittAuthor Commented:
I see no way of doing this. The only way I find to set the field type is before databinding occurs and is done by manually adding a column type. However, at rowdatabound there is no columns collection in the gridview object. Nor at any other event. I set up breakpoints in various events (even at page unload) and checked for GridView1.Columns.Count and it always returns 0. The data is being displayed, however. GridView1.Rows.Count returns a value, like GridView1.Rows(0).Cells.Count. But the columns aren't being created, despite the autogeneratecolumns property being set to true.
0
 
CluskittAuthor Commented:
So, in order to define different types of column types, I have to manually generate them? I guess that would involve creating the query in code behind, checking for number of columns and adding them accordingly before databinding them.

From my end, it would be easier to just set all column types to textbox instead of label and leave the autogeneratecolumns as true. Is that possible?
0
 
CluskittAuthor Commented:
I had to actually add the columns manually before databound. I ended up generating the dynamic sql, then generating the columns in the gridview dynamically, then databinding. Seems to work decently.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.