This is a migrated thread and some comments may be shown as answers.

Format data value in GridBoundColumn

6 Answers 327 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mats
Top achievements
Rank 1
Mats asked on 14 Sep 2009, 11:05 AM
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

6 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 16 Sep 2009, 01:13 PM
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.
0
Ralf
Top achievements
Rank 1
answered on 10 Dec 2011, 02:15 PM
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
0
Veli
Telerik team
answered on 12 Dec 2011, 05:57 PM
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
0
Ralf
Top achievements
Rank 1
answered on 13 Dec 2011, 07:45 AM
Hello Veli,

thank you for information. I will check this out.


Best regards,
Ralf

0
Ralf
Top achievements
Rank 1
answered on 20 Dec 2011, 06:44 PM
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
0
Veli
Telerik team
answered on 21 Dec 2011, 01:07 PM
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
Tags
Grid
Asked by
Mats
Top achievements
Rank 1
Answers by
Veli
Telerik team
Ralf
Top achievements
Rank 1
Share this question
or