Samer Ibrahim's Blog

The Samer I Warrior on battles with .NET

Sponsors

Lists/Forums/Etc.

Which Blogs do I read?

ASP.NET DataGrid's AutoGenerateColumns doesn't add the BoundColumns to the Columns collection... WHAT!!

While I ordinarily set AutoGenerateColumns = false on ASP.NET DataGrids, I finally found a pretty good use for them.  I had a result set that was coming back with a variable number of columns all of which need to be display.  I thought what a perfect use for the auto-generating columns but low and behold I discovered the documentation says:

Note   Explicitly declared columns may be used in conjunction with auto-generated columns. When using both, explicitly declared columns will be rendered first, followed by the auto-generated columns. Auto-generated columns are not added to the Columns collection.

The hell kind of garbage is that?!?!  Why aren't Auto-generated columns added to the Columns collection?!?!?  Shouldn't they abide by the same rules other columns do?  After all, aren't they just BoundColumn's?  All I wanted to do was simply set the DataFormatString property on them.  What an annoyance!  Instead I had to check the number of columns in the IDataReader using the FieldCount property and then programmatically creating each column and set the format.  But then what is the point of the AutoGenerateColumns property?  Has anyone ever used it in a real world application?  Oh well.....

Comments

TrackBack said:

# February 13, 2004 7:18 PM

Datagrid Girl said:

Hi Samer,
You could also access the data during the ItemDataBound event--though they don't appear in the .Columns() collection, you still have the .Cells() collection of each DataGridItem that you can work with to do formatting, etc.

Hope that helps!
Marcie
# February 15, 2004 6:57 PM

Chris McKenzie said:

But can you still make toggle the column visibility that way?

Chris
# February 16, 2004 8:23 AM

WeSam Abdallah said:

It is very annoying... I have a simular situation where I want to change the order of the Autogenerated columns to appear prior to the Edit/Update column.. which I added in the aspx code explicitly..

Man it is such a pain, I tried to turn the AutoGenerateColumns to false and add my columns to the column collection and I ended up with another problem... for some reason DataGrid wasn't saving my added columns at run time into the Grid View State, so I have to add them at every post back now, is the silly or what..

you can Enable/Disable ViewState on rows but not columns.

WeSam Abdallah

# August 3, 2007 7:38 AM

BloodyBenny said:

That is super annoying, and it doesn't make any sense. It's a road block for me right now, because I'm binding a DataTable to the GridView, and I want to sort on a particular column but not actually display the column. The sorting works, but there's no way to turn off visibility for the entire column because I can't access it via the Columns collection.

Yeargh!

# January 23, 2009 11:38 AM

Yigit Haciefendioglu said:

I am also dealing with the same problem right now. I want my update and delete image buttons (which are template fields) to locate at the very end of the columns but auto generated columns surpass them.

I want to like asp.net, I really do, but somehow the hatred against it become more dominant..

I generally end up not using asp.net's wizards, autogenerate things but if we won't use them, why the hell are we programming on .net framework??

# January 11, 2011 12:15 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)