DataFormatString not getting applied to GridView's BoundField

T

TS

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(DomainObjectListKey);
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);
}

}
 
S

Steven Cheng[MSFT]

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/2005/10/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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.

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


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

TS

thanks steven that was it.


Steven Cheng said:
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/2005/10/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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.

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


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

Phil H

thanks steven that was it.












- 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.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top