DataFormatString not getting applied to GridView's BoundField

Discussion in 'ASP .Net Web Controls' started by TS, Dec 18, 2007.

  1. TS

    TS Guest

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

    }
     
    TS, Dec 18, 2007
    #1
    1. Advertising

  2. 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.
    --------------------
    >From: "TS" <>
    >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(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);
    >}
    >
    >}
    >
    >
    >
     
    Steven Cheng[MSFT], Dec 19, 2007
    #2
    1. Advertising

  3. TS

    TS Guest

    thanks steven that was it.


    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > 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.
    > --------------------
    >>From: "TS" <>
    >>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(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);
    >>}
    >>
    >>}
    >>
    >>
    >>

    >
     
    TS, Dec 19, 2007
    #3
  4. TS

    Phil H Guest

    On 19 Dec, 13:55, "TS" <> wrote:
    > thanks steven that was it.
    >
    > "Steven Cheng[MSFT]" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > 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.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/subscriptions/support/default.aspx.

    >
    > > ==================================================

    >
    > > This posting is provided "AS IS" with no warranties, and confers no
    > > rights.
    > > --------------------
    > >>From: "TS" <>
    > >>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(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);
    > >>}

    >
    > >>}- 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.
     
    Phil H, Dec 22, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    2,864
  2. yefei

    BoundField and DataFormatString

    yefei, Mar 28, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    4,940
    yefei
    Mar 28, 2006
  3. Jimmy J.
    Replies:
    1
    Views:
    406
    Mark Rae
    Jan 10, 2007
  4. Replies:
    2
    Views:
    1,211
  5. Abraham Andres Luna

    gridview.boundfield.dataformatstring doesn't work

    Abraham Andres Luna, Nov 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    370
    Ing. Winkler Bernhard
    Dec 2, 2005
Loading...

Share This Page