473,762 Members | 8,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unable to set DataFormatStrin g (in code) on GridView with AutoGenerateCol umns

hi,
with a derived DataGrid control, i can override the CreateColumnSet method
and scan into the DataGridColumns and set the DataFormatStrin g, 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 DataFormatStrin g on an AutoGeneratedFi eld.

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

protected override ICollection CreateColumns(P agedDataSource dataSource,
bool useDataSource)
{
ArrayList arr = (ArrayList)base .CreateColumns( dataSource, useDataSource);
foreach (AutoGeneratedF ield f in arr)
f.DataFormatStr ing = "{0:dd/MM/yyyy}"; // throws exception
return arr;
}
}

here's the exception:

[NotSupportedExc eption: Specified method is not supported.]
System.Web.UI.W ebControls.Auto GeneratedField. set_DataFormatS tring(String
value) +1839359
SmartGridView.C reateColumns(Pa gedDataSource dataSource, Boolean
useDataSource)

it makes no difference if i cast the AutoGeneratedFi eld 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 6520
Tim,

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

public override string DataFormatStrin g
{
get
{
return base.DataFormat String;
}
set
{
if (!this._suppres sPropertyThrows )
{
throw new NotSupportedExc eption();
}
}
}

Which is quite logical as the AutoGeneratedFi eld 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 _suppressProper tyThrows field (not
recommended
2. (recommended) override
CreateAutoGener atedColumns(Pag edDataSource dataSource) and/or
CreateAutoGener atedColumn(Auto GeneratedFieldP roperties fieldProperties )
methods to provide AutoGeneratedFi eld with DataFormatStrin g 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 DataFormatStrin g, 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 DataFormatStrin g on an AutoGeneratedFi eld.

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

protected override ICollection CreateColumns(P agedDataSource dataSource,
bool useDataSource)
{
ArrayList arr = (ArrayList)base .CreateColumns( dataSource, useDataSource);
foreach (AutoGeneratedF ield f in arr)
f.DataFormatStr ing = "{0:dd/MM/yyyy}"; // throws exception
return arr;
}
}

here's the exception:

[NotSupportedExc eption: Specified method is not supported.]
System.Web.UI.W ebControls.Auto GeneratedField. set_DataFormatS tring(String
value) +1839359
SmartGridView.C reateColumns(Pa gedDataSource dataSource, Boolean
useDataSource)

it makes no difference if i cast the AutoGeneratedFi eld 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
DataFormatStrin g.

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:SmartGri d runat="server" id="SmartGrid1 " /so there are no BoundFields
declared in the markup. also, i tried casting the AutoGeneratedFi eld 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
AutoGeneratedFi eld objects.

any ideas?
thanks
tim

Sep 26 '07 #4
Hi Tim,

Sorry for the misunderstandin g.

In this case, since you're creating a customized GridView control and you
need to control each field's DataFormatStrin g, regardless if the field is
auto-generated or not, not being able to set DataFormatStrin g of
AutoGeneratedFi eld 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.comwr ote in message
news:Yu******** *****@TK2MSFTNG HUB02.phx.gbl.. .
Hi Tim,

Sorry for the misunderstandin g.

In this case, since you're creating a customized GridView control and you
need to control each field's DataFormatStrin g, regardless if the field is
auto-generated or not, not being able to set DataFormatStrin g of
AutoGeneratedFi eld 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
1619
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" DataFormatString="{0:yyyy/MM/dd}" It still shows the date as "2005/05/03 12:45 AM" It worked in beta 2, don't knwo about July CTP, but August CTP is a problem v2.0.50727
1
2005
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 Details view does not work. Runtime v2.0.50727. <asp:BoundField DataField="Event_Date" DataFormatString="{0:#0.0}" It still shows the value as 23.04
1
6637
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 CssClass="ItemCaption" /> <RowStyle CssClass="Line" /> <AlternatingRowStyle CssClass="AltLine" /> <Columns> <asp:BoundField DataField="ItemName" HeaderText="Item
1
1402
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
1447
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. My GridView defined like,
2
1087
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 coming in. Help?
2
2089
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 14:23:28". I want to display it as "03/30/02". I've included the code below. Can anyone offer an explanation? <asp:SqlDataSource ID="DD_Issues" runat="server"
1
1910
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
4850
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 columnscollection and get the row count, but for some reasons the gridview does not get autopoulated with the results. The Gridview is bound to objectdatasource and I can see all the events firing as follows when I trace: objdsCourseMatrix_Selecting...
11
2587
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 question is: Can i add the columns at runtime without doing AutoGenerateColumns=True. Plz Suggest me anykind of alternatives. Thnks in advance
0
9378
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8814
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7360
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5268
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.