Format data value in GridBoundColumn

7 posts, 0 answers
  1. Mats
    Mats avatar
    66 posts
    Member since:
    Aug 2006

    Posted 14 Sep 2009 Link to this post

    Hi!
    I have a general problem when binding my data to a grid and getting all functionality to work smoothly without to much hands on.

    What I do is that I have data that I want to display and use built in functionality like sorting, filtering and grouping.
    My data is not always in a presentable shape so I need to format the data output before presenting it in a column. This is not a problem since I can use templates or FormatDataValue in a derived GridBoundColumn. The problem is that the sorting, filtering and grouping will not work without lot's of specific coding.

    I would like to find out a way to in one place (like a derived GridBoundColumn) take care of the conversion/presentation of the data value in a way that it will more or less automatically be mirrored in filtering, sorting and grouping.

    As an example I sometimes store time as an integer for minutes. When showing the data value I convert it to "mm:ss". The problem is now that the GroupHeaderItem is showing the integer and the filter box is working with the integer as well.

    Any suggestions on how to solve this in a generic way?

    Thanks
    Mats
  2. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 16 Sep 2009 Link to this post

    Hi Mats,

    Unfortunately, you need to manually format all values that are misrepresented, as you do so far. The reason for this is that whenever this value is need for display (either in the databound cell, or in the group header), the original DataItem object bound to the GridDataItem is used to extract the value. This means that even though you customize the representation of the value in your data source when displayed in the column cell, RadGrid still uses the original value extracted from the data item for filtering, grouping, sorting, etc.

    If you need truly seamless integration of custom formatting of data values in RadGrid, consider extracting the values from your data source, formatting them to your liking and saving them in another data object that you provide as RadGrid's data source. Thus, RadGrid now has the correct representation of the data to bind to and all displayed values should be in the correct format.

    Sincerely yours,
    Veli
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Ralf
    Ralf avatar
    77 posts
    Member since:
    Jun 2012

    Posted 10 Dec 2011 Link to this post

    Hi,

    so, the group header will never use the formatted data value? Why?
    For example, in my scenario the datatable includes a datatime column. I have overwritten the FormatDataValue method in my column class to display only the date part of values (10.12.2011 will be displayed for example). When I group by that column the header text displays Quotation Date: 10.12.2011 00:00:00 (x of y entries grouped). But I need to see Quotation Date: 10.12.2011 (x of y entries grouped).

    Another example. We are developing solutions for SharePoint. The SPSiteDataQuery returns only datatable including string typed values.
    The column "Created by" containing values like "1;#hostname\username". I have overwritten the FormatDataValue to display only "hostname\username" in the cells. When I group by that column, the group header displays "Created by: 1;#hostname\username". I need to display "Created by: hostname\username". For related columns we use SPField formatting methods in the overwritten FormatDataValue method.

    Note that we have created the grid and gridcolumns programmatically, not declarative because of dynamic reasons.

    How to get the needed behavior?

    Best regards,
    Ralf
  5. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 12 Dec 2011 Link to this post

    Hi Ralf,

    You can format the grouped value in your group headers by specifying the FormatString property of the GridGroupByField object you add to the SelectFields collection of a group expression. Here is the declarative version:

    <telerik:RadGrid>
        <MasterTableView>
            <GroupByExpressions>
                <%--Date is the name of a date column--%>
                <telerik:GridGroupByExpression>
                    <GroupByFields>
                        <telerik:GridGroupByField FieldName="Date" HeaderText="Date" />
                    </GroupByFields>
                    <SelectFields>
                        <telerik:GridGroupByField FieldName="Date" FormatString="{0:d}" />
                    </SelectFields>
                </telerik:GridGroupByExpression>
            </GroupByExpressions>
        </MasterTableView>
    </telerik:RadGrid>


    Veli
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
  6. Ralf
    Ralf avatar
    77 posts
    Member since:
    Jun 2012

    Posted 13 Dec 2011 Link to this post

    Hello Veli,

    thank you for information. I will check this out.


    Best regards,
    Ralf

  7. Ralf
    Ralf avatar
    77 posts
    Member since:
    Jun 2012

    Posted 20 Dec 2011 Link to this post

    Hi Veli,

    could you please explain how to do this programmatically?
    All columns will be created programmatically by implementing special column classes inherits from rag columns.

    Thank you!

    Best regards,
    Ralf
  8. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 21 Dec 2011 Link to this post

    Hello Ralf,

    The example suggests adding a FormatString property to your group expressions. This particularly is not related to grid columns. If you need to add group expressions programmatically, please refer to the RadGrid Group By Expressions Programmatic Definition help topic.

    Greetings,
    Veli
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017