HighTechTalks DotNet Forums  

DataFormatString not getting applied to GridView's BoundField

ASP.net Web Controls microsoft.public.dotnet.framework.aspnet.webcontrols


Discuss DataFormatString not getting applied to GridView's BoundField in the ASP.net Web Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
TS
 
Posts: n/a

Default DataFormatString not getting applied to GridView's BoundField - 12-18-2007 , 09:48 AM







Hello, I have a control that inherits from GridView and i'm overriding
databinding. My control works correctly but is not respecting the
DataFormatString property that i'm setting on the field. As you will notice,
i am using column information from meta data. The actual format string from
meta data is "{0:d}" and the field type is a dateTime show it should show a
short date.

Why isn't this one property formatting correctly?

thanks

protected override void OnDataBinding(EventArgs e){
IList list = Utils.Utils.GetDomainObjectList(DomainObjectListKe y);
bool gridViewHasDataRows = (list != null && list.Count > 0);
CreateColumns(gridViewHasDataRows);
if (DataSource == null)
DataSource = list;
base.OnDataBinding(e);
}

protected void CreateColumns(bool gridViewHasDataRows)
{
if (MetaData == null) MetaData =
GridViewMetaData.GetGridViewMetaData(MetaDataId);
DataControlField dcf = new BoundField();
Columns.Clear();
foreach (GridViewColumnMetaData columnMetaData in MetaData.Columns)
{
((BoundField)dcf).DataField = columnMetaData.PropertyName;
((BoundField)dcf).DataFormatString = columnMetaData.DataFormatString;
//Here is the line that I am setting it
dcf.HeaderText = columnMetaData.HeaderText;
if (columnMetaData.IsSortable) dcf.SortExpression =
columnMetaData.SortPropertyName;
dcf.Visible = columnMetaData.IsDisplayed;
dcf.ItemStyle.HorizontalAlign = columnMetaData.HorizontalAlign;
dcf.ItemStyle.Width = columnMetaData.Width;
Columns.Add(dcf);
}

}



Reply With Quote
  #2  
Old   
Steven Cheng[MSFT]
 
Posts: n/a

Default RE: DataFormatString not getting applied to GridView's BoundField - 12-18-2007 , 10:07 PM






Hi TS,

From your description, I understand you've created a custom GridView class
derived from built-in one. And in its code you programmatically populate
all the columns through some info in metadata. However, you found the
boundfield(which has DataFormatString set) does not format output as
expected, correcct?

Based on my experience, ASP.NET GridView control has a common issue about
DataformatString. That is the "HtmlEncode" setting for BoundField. By
deafult it is turn on which will cause the "DataFormatString" not applied.
You will need to turn it off so as to make DataFormatString applied. Here
is a blog article also mentioned this:

#BoundField DataFormatString attribute not being applied.
http://weblogs.asp.net/rajbk/archive...31/429090.aspx

You can try it to see whether it helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
Quote:
From: "TS" <manofsteele1 (AT) nospam (DOT) nospam
Subject: DataFormatString not getting applied to GridView's BoundField
Date: Tue, 18 Dec 2007 09:48:00 -0600

Hello, I have a control that inherits from GridView and i'm overriding
databinding. My control works correctly but is not respecting the
DataFormatString property that i'm setting on the field. As you will
notice,
i am using column information from meta data. The actual format string
from
meta data is "{0:d}" and the field type is a dateTime show it should show
a
short date.

Why isn't this one property formatting correctly?

thanks

protected override void OnDataBinding(EventArgs e){
IList list = Utils.Utils.GetDomainObjectList(DomainObjectListKe y);
bool gridViewHasDataRows = (list != null && list.Count > 0);
CreateColumns(gridViewHasDataRows);
if (DataSource == null)
DataSource = list;
base.OnDataBinding(e);
}

protected void CreateColumns(bool gridViewHasDataRows)
{
if (MetaData == null) MetaData =
GridViewMetaData.GetGridViewMetaData(MetaDataId);
DataControlField dcf = new BoundField();
Columns.Clear();
foreach (GridViewColumnMetaData columnMetaData in MetaData.Columns)
{
((BoundField)dcf).DataField = columnMetaData.PropertyName;
((BoundField)dcf).DataFormatString = columnMetaData.DataFormatString;
//Here is the line that I am setting it
dcf.HeaderText = columnMetaData.HeaderText;
if (columnMetaData.IsSortable) dcf.SortExpression =
columnMetaData.SortPropertyName;
dcf.Visible = columnMetaData.IsDisplayed;
dcf.ItemStyle.HorizontalAlign = columnMetaData.HorizontalAlign;
dcf.ItemStyle.Width = columnMetaData.Width;
Columns.Add(dcf);
}

}





Reply With Quote
  #3  
Old   
TS
 
Posts: n/a

Default Re: DataFormatString not getting applied to GridView's BoundField - 12-19-2007 , 07:55 AM



thanks steven that was it.


"Steven Cheng[MSFT]" <stcheng (AT) online (DOT) microsoft.com> wrote

Quote:
Hi TS,

From your description, I understand you've created a custom GridView class
derived from built-in one. And in its code you programmatically populate
all the columns through some info in metadata. However, you found the
boundfield(which has DataFormatString set) does not format output as
expected, correcct?

Based on my experience, ASP.NET GridView control has a common issue about
DataformatString. That is the "HtmlEncode" setting for BoundField. By
deafult it is turn on which will cause the "DataFormatString" not applied.
You will need to turn it off so as to make DataFormatString applied. Here
is a blog article also mentioned this:

#BoundField DataFormatString attribute not being applied.
http://weblogs.asp.net/rajbk/archive...31/429090.aspx

You can try it to see whether it helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
From: "TS" <manofsteele1 (AT) nospam (DOT) nospam
Subject: DataFormatString not getting applied to GridView's BoundField
Date: Tue, 18 Dec 2007 09:48:00 -0600

Hello, I have a control that inherits from GridView and i'm overriding
databinding. My control works correctly but is not respecting the
DataFormatString property that i'm setting on the field. As you will
notice,
i am using column information from meta data. The actual format string
from
meta data is "{0:d}" and the field type is a dateTime show it should show
a
short date.

Why isn't this one property formatting correctly?

thanks

protected override void OnDataBinding(EventArgs e){
IList list = Utils.Utils.GetDomainObjectList(DomainObjectListKe y);
bool gridViewHasDataRows = (list != null && list.Count > 0);
CreateColumns(gridViewHasDataRows);
if (DataSource == null)
DataSource = list;
base.OnDataBinding(e);
}

protected void CreateColumns(bool gridViewHasDataRows)
{
if (MetaData == null) MetaData =
GridViewMetaData.GetGridViewMetaData(MetaDataId);
DataControlField dcf = new BoundField();
Columns.Clear();
foreach (GridViewColumnMetaData columnMetaData in MetaData.Columns)
{
((BoundField)dcf).DataField = columnMetaData.PropertyName;
((BoundField)dcf).DataFormatString = columnMetaData.DataFormatString;
//Here is the line that I am setting it
dcf.HeaderText = columnMetaData.HeaderText;
if (columnMetaData.IsSortable) dcf.SortExpression =
columnMetaData.SortPropertyName;
dcf.Visible = columnMetaData.IsDisplayed;
dcf.ItemStyle.HorizontalAlign = columnMetaData.HorizontalAlign;
dcf.ItemStyle.Width = columnMetaData.Width;
Columns.Add(dcf);
}

}







Reply With Quote
  #4  
Old   
Phil H
 
Posts: n/a

Default Re: DataFormatString not getting applied to GridView's BoundField - 12-21-2007 , 06:31 PM



On 19 Dec, 13:55, "TS" <manofstee... (AT) nospam (DOT) nospam> wrote:
Quote:
thanks steven that was it.

"Steven Cheng[MSFT]" <stch... (AT) online (DOT) microsoft.com> wrote in message

news:YVuUKTfQIHA.4200 (AT) TK2MSFTNGHUB02 (DOT) phx.gbl...



Hi TS,

From your description, I understand you've created a custom GridView class
derived from built-in one. And in its code you programmatically populate
all the columns through some info in metadata. However, you found the
boundfield(which has DataFormatString set) does not format output as
expected, correcct?

Based on my experience, ASP.NET GridView control has a common issue about
DataformatString. That is the "HtmlEncode" setting for BoundField. By
deafult it is turn on which will cause the "DataFormatString" not applied.
You will need to turn it off so as to make DataFormatString applied. Here
is a blog article also mentioned this:

#BoundField DataFormatString attribute not being applied.
http://weblogs.asp.net/rajbk/archive...31/429090.aspx

You can try it to see whether it helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ps/default.asp...
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
From: "TS" <manofstee... (AT) nospam (DOT) nospam
Subject: DataFormatString not getting applied to GridView's BoundField
Date: Tue, 18 Dec 2007 09:48:00 -0600

Hello, I have a control that inherits from GridView and i'm overriding
databinding. My control works correctly but is not respecting the
DataFormatString property that i'm setting on the field. As you will
notice,
i am using column information from meta data. The actual format string
from
meta data is "{0:d}" and the field type is a dateTime show it should show
a
short date.

Why isn't this one property formatting correctly?

thanks

protected override void OnDataBinding(EventArgs e){
IList list = Utils.Utils.GetDomainObjectList(DomainObjectListKe y);
bool gridViewHasDataRows = (list != null && list.Count > 0);
CreateColumns(gridViewHasDataRows);
if (DataSource == null)
DataSource = list;
base.OnDataBinding(e);
}

protected void CreateColumns(bool gridViewHasDataRows)
{
if (MetaData == null) * *MetaData =
GridViewMetaData.GetGridViewMetaData(MetaDataId);
DataControlField dcf = new BoundField();
Columns.Clear();
foreach (GridViewColumnMetaData columnMetaData in MetaData.Columns)
{
((BoundField)dcf).DataField = columnMetaData.PropertyName;
((BoundField)dcf).DataFormatString = columnMetaData.DataFormatString;
//Here is the line that I am setting it
dcf.HeaderText = columnMetaData.HeaderText;
if (columnMetaData.IsSortable) *dcf.SortExpression =
columnMetaData.SortPropertyName;
dcf.Visible = columnMetaData.IsDisplayed;
dcf.ItemStyle.HorizontalAlign = columnMetaData.HorizontalAlign;
dcf.ItemStyle.Width = columnMetaData.Width;
Columns.Add(dcf);
}

}- Hide quoted text -

- Show quoted text -
As a point of general interest I too have experienced the problem that
ordinary bound Label controls displaying DateTime information do not
implement formatting. The column has to be "converted" to a "template"
and the "TwoWay binding" check box unchecked in the databinding wizard
(so the Eval rather than the Bind function is used).

I think it may have something to do with parsing the text during edit
mode (textbox entry by default for a non-templated column) where the
date and time format is determined by the user. This may give rise to
an anomaly if the format varies according to the direction of data
flow. Just a guess though.


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.