Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > DataFormatString not getting applied to GridView's BoundField

Reply
Thread Tools

DataFormatString not getting applied to GridView's BoundField

 
 
TS
Guest
Posts: n/a
 
      12-18-2007

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
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      12-19-2007
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" <(E-Mail Removed)>
>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
 
 
 
 
TS
Guest
Posts: n/a
 
      12-19-2007
thanks steven that was it.


"Steven Cheng[MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)>
>>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
 
Phil H
Guest
Posts: n/a
 
      12-22-2007
On 19 Dec, 13:55, "TS" <(E-Mail Removed)> wrote:
> thanks steven that was it.
>
> "Steven Cheng[MSFT]" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
> > 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" <(E-Mail Removed)>
> >>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

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Gridview BoundField DataFormatString question ronc85@gmail.com ASP .Net 2 08-26-2007 06:32 PM
DataFormatString does not work in Boundfield Jimmy J. ASP .Net 1 01-10-2007 11:09 PM
BoundField and DataFormatString yefei ASP .Net 2 03-28-2006 02:21 PM
BoundField and DataFormatString in ASP.NET 2.0 - Bug ? Ing. Winkler Bernhard ASP .Net Web Controls 3 12-07-2005 03:50 PM
BoundField.DataFormatString and length of displayed string pawel.pabich@gmail.com ASP .Net 0 11-07-2005 02:45 PM



Advertisments