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

DataFormatString

3 Answers 802 Views
GridView
This is a migrated thread and some comments may be shown as answers.
TSRG - IT
Top achievements
Rank 1
TSRG - IT asked on 24 Nov 2009, 07:47 AM
Hi guys,

I've set up some columns which are of Decimal data type with a data format string which converts these decimals values to a percentage between 0 - 100% ie. DataFormatString = "{0.0.00\\%}". The problem I am having is a few of these rows do not need to have the data format string on them, they should be left as they are. I've had a look at conditional formatting with Converters with the ContentStringFormat but can't get this to work (I believe because the data type is decimal?). I am using the decimal type because this helps with validation (only numerics). Otherwise, what's the best method to implement this?

Thanks!

Tim.

This is the code I used for the converter:

XAML

<

 

telerikGrid:RadGridView.Resources>

 

 

 

<Client:ValueConverter x:Key="ValueConverter"/>

 

 

 

<Style TargetType="{x:Type telerik:GridViewCell }">

 

 

 

<Setter Property="ContentStringFormat" Value="{Binding Path=Period,Converter={StaticResource ValueConverter}}"/>

 

 

 

</Style>

 

 

 

</telerikGrid:RadGridView.Resources>

 


Binding Path=Period is a <BindingList> of Decimals so 'object value' returns an array of objects, which I have to extract. Basically my goal is to overwrite (or bypass) the DataFormatString in the GridViewDataColumn for a few rows - if this is possible.

Converter Class

public

 

object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

 

{

 

//if (a[0] > 100)

 

 

return "{0:0.00}";

 

 

//return Binding.DoNothing;

 

}


3 Answers, 1 is accepted

Sort by
0
Tsvyatko
Telerik team
answered on 24 Nov 2009, 04:55 PM
Hi Timothy Lin,

You can customize how the data is displayed using the value converters. They can completely replace the DataFormatString functionality. However you can use both value converters and DataFormatString.

I am attaching sample application that demonstrates simultaneously usage of DataFormatString and value converter.

If you have any more questions do not hesitate to contact us.

Best wishes,
Tsvyatko
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
TSRG - IT
Top achievements
Rank 1
answered on 25 Nov 2009, 07:10 AM

Thanks Tsvyatko, that worked like a charm. Just had a look at my implementation and it seems that the condition I need when it needs to apply formatting depends on the value in the same row but in another column. So basically I have to check the value in another column called "Value" and if it is "A" for example, then format one way or if it is "B" then format another way. Is there a way to do this via the Converter?

What I have done at the moment is disable the DataFormatString altogether, and I've used the following snippet of code to set the cell's content in the Row_Loaded event:

 

private

 

void OutcomeGridView_RowLoaded(object sender, RowLoadedEventArgs e)

 

{
...

 

cell.Content =

String.Format("{0:0.00%}", cell.Content);
...
}

This works fine as I am able to conditionally apply this however, whenever I double click onto a cell to edit it and then press enter or click out of the cell, the cell loses the formatting and reverts back to its underlying value. I've tried to explicitly set the Content property in the Cell Edit Ended event handler:

 

 

private void OutcomeGridView_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
{
...

 

e.Cell.Content =

String.Format("{0:0.00%}", e.Cell.Content);

 

 

..
}

But this doesn't work, any ideas?

Cheers,

Tim.

0
Accepted
Tsvyatko
Telerik team
answered on 26 Nov 2009, 12:50 PM
Hello Timothy Lin,

Attached is a sample application with some modifications which allows the following:

 - Cell data format is based on the values in the other cells
 - Cells data is editable and updates its format when other cell values are updated

The steps you need to perform:
 - Implement Convertback method in your ValueConverter
 - Apply the format you needed in RowLoaded event
 - Attach to CellValidating to set the data format that Converter will use after the data is updated

I hope this works work for you.
Please let me know if you have any other questions or need some more help.

P.S. If you do not need the editing you can pass the whole object to the converter to customize your data


Regards,
Tsvyatko Konov
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.
Tags
GridView
Asked by
TSRG - IT
Top achievements
Rank 1
Answers by
Tsvyatko
Telerik team
TSRG - IT
Top achievements
Rank 1
Share this question
or