473,433 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,433 software developers and data experts.

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

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_D ataFormatString(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

Sep 20 '07 #1
5 6489
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(AutoGeneratedFieldProper ties 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

--
Milosz
"Tim Mackey" wrote:
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_D ataFormatString(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
Sep 20 '07 #2
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/co...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 (wa****@online.microsoft.com, 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.
Sep 21 '07 #3
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

Sep 26 '07 #4
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/co...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...-gridview.html

Regards,
Walter Wang (wa****@online.microsoft.com, 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.

Sep 28 '07 #5
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
""Walter Wang [MSFT]"" <wa****@online.microsoft.comwrote in message
news:Yu*************@TK2MSFTNGHUB02.phx.gbl...
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/co...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...-gridview.html

Regards,
Walter Wang (wa****@online.microsoft.com, 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.
Oct 1 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gerhard Pretorius | last post by:
Since installing the VS.NET August CTP the DataFormatString of the BoundField in the Gridview does not work. Runtime v2.0.50727. <asp:BoundField DataField="Event_Date"...
1
by: Gerhard Pretorius | last post by:
I am reposting this..hoping someone (from Microsoft) can confirm this at least as a problem: Since installing the VS.NET August CTP the DataFormatString of the BoundField in the Gridview and...
1
by: Hardy Wang | last post by:
Hi, I have a GridView control in my web form (ASP.NET 2.0) <asp:GridView ID="gvReport" runat="server" AllowSorting="True" AutoGenerateColumns="False" Width="100%"> <HeaderStyle...
1
by: Tina | last post by:
Putting {0:d} in the DataFormatString for a date field in a GridView doesn't work. That date still shows with 12:00:00 AM and all of that. T
0
by: Sachin | last post by:
Hi All, I am using ASP.NET 2.0 and GridView control. I have a stored procedure, SP_GetAllRows which takes the table name as parameter and fires the query "Select * from TableName" dynamically....
2
by: Sean | last post by:
DataFormatString="{0:d}" for my boundcolumn should format my datetime datatype as xx/xx/xxx instead of xx/xx/xxxx xx:xx:xxAM only trouble is...it doesnt change the format of my DateTime field...
2
by: pgonzo | last post by:
In my initial tests with the GridView control, I cannot understand why the DataFormatString attribute has no affect on the column formatting. The 'creation_date' field is being reported as "3/30/02...
1
by: yogarajan.ganesan | last post by:
hello friend am new in asp.net i have one doubt pls help me page load: DataSet ds = BuildDataSet(); DataGrid1.DataSource = ds; DataGrid1.DataBind();
2
by: Aamir Ghanchi | last post by:
Hello, I would like to hear if any one else is having the same problem. The AutoGenerateColumns property is set to true. I can read the data right and can loop through the datasource's...
11
by: giveDsolution | last post by:
Hope to find the Solution, My requirment is, I have set AutoGenerateColumns=True for a gridview as i have to add columns at runtime. My first question is: How to hide a column ? And my second...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.