• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

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?
0
Cluskitt
Asked:
Cluskitt
  • 4
  • 3
1 Solution
 
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
 
CodeCruiserCommented:
Give that a try.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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
 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now