unable to set DataFormatString (in code) on GridView with AutoGenerateColumns

T

Tim Mackey

hi,
with a derived DataGrid control, i can override the CreateColumnSet method
and scan into the DataGridColumns and set the DataFormatString, e.g. leave
out the time part of a DateTime value. this works great for a DataGrid
control.

however i'm upgrading this control to inherit from GridView. there seems to
be a limitation of the gridview "Method not supported" when you try and set
a DataFormatString on an AutoGeneratedField.

here's the code:
[ToolboxData("<{0}:SmartGridView runat=server></{0}:SmartGridView>")]
public class SmartGridView : GridView
{
public SmartGridView()
{
}

protected override ICollection CreateColumns(PagedDataSource dataSource,
bool useDataSource)
{
ArrayList arr = (ArrayList)base.CreateColumns(dataSource, useDataSource);
foreach (AutoGeneratedField f in arr)
f.DataFormatString = "{0:dd/MM/yyyy}"; // throws exception
return arr;
}
}

here's the exception:

[NotSupportedException: Specified method is not supported.]
System.Web.UI.WebControls.AutoGeneratedField.set_DataFormatString(String
value) +1839359
SmartGridView.CreateColumns(PagedDataSource dataSource, Boolean
useDataSource)

it makes no difference if i cast the AutoGeneratedField to a BoundField.
does anyone know how you can do this? seems strange that the GridView would
lose some features of the DataGrid.

thanks in advance
tim
 
G

Guest

Tim,

Unfortunatelly, this property is overriden and can only be set internally
when
_suppressPropertyThrows field is set to true:

public override string DataFormatString
{
get
{
return base.DataFormatString;
}
set
{
if (!this._suppressPropertyThrows)
{
throw new NotSupportedException();
}
}
}

Which is quite logical as the AutoGeneratedField class is a sealed wrapper
around the BoundField class introduced to represent nonchangable field. There
are two ways you could overcome the problem:
1. use reflection to change value of the _suppressPropertyThrows field (not
recommended
2. (recommended) override
CreateAutoGeneratedColumns(PagedDataSource dataSource) and/or
CreateAutoGeneratedColumn(AutoGeneratedFieldProperties fieldProperties)
methods to provide AutoGeneratedField with DataFormatString set as needed.
In addition to taht, download very powerful tool called Reflector:
http://www.aisto.com/roeder/dotnet/
so you can see the Grid's View code yourself (as well as any other class
from any assembly).

HTH
 
W

Walter Wang [MSFT]

Thanks Milosz for your informative input.

Hi Tim,

Thanks for your feedback. Indeed this is by design behavior of the auto
generated field. You can use a BoundField instead and set the
DataFormatString.

Please kindly submit your feedback here which is monitored by our product
group:

http://connect.microsoft.com/Main/content/content.aspx?ContentID=2220

In this way, customers like you who have similiar requirement can vote on a
feedback and raise priority of such feature request.

Please feel free to let me know if there's anything else I can help.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

Tim Mackey

hi Walter,
thanks for the follow up. maybe i didn't make clear that i'm doing this
programatically, the only ASPX available is
<cc1:SmartGrid runat="server" id="SmartGrid1" /> so there are no BoundFields
declared in the markup. also, i tried casting the AutoGeneratedField to a
BoundColumn but it makes no difference, you still get the exception.
if you inspect the items in the ArrayList in my code, they are indeed
AutoGeneratedField objects.

any ideas?
thanks
tim
 
W

Walter Wang [MSFT]

Hi Tim,

Sorry for the misunderstanding.

In this case, since you're creating a customized GridView control and you
need to control each field's DataFormatString, regardless if the field is
auto-generated or not, not being able to set DataFormatString of
AutoGeneratedField does look too restrictive. I've recorded your feature
request and forwarded to product group. In the meanwhile, you're encouraged
to submit this at
http://connect.microsoft.com/Main/content/content.aspx?ContentID=2220 so
that you can get prompt response when product group responses.

In the meanwhile, although not recommended and supported (as Milosz already
pointed out, thanks), you could use reflection to change it:

#Siderite Zackwehdex's Blog: How to format your GridView autogenerated
columns
http://siderite.blogspot.com/2006/09/how-to-format-your-gridview.html

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

Tim Mackey

hi milosz, thanks for sharing this tip, i tried your code and it works
great.
thanks for the follow up walter, i'll do the MS connect thing.
thanks
tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top