Reputation: 4898
So I have a dynamic RadGridView. This means that I add Columns programmatically to the control. basically like this:
for (int i = 0; i < length; i++)
{
Binding b = new Binding(string.Format("Collection[{0}].ValueIWant", i));
binding.StringFormat = "{0:0.##}";
GridViewDataColumn column = new GridViewDataColumn()
{
Header = HeaderFor(i),
DataMemberBinding = b,
DataType = typeof(double?)
};
Control.columns.Add(column);
}
Now I need to add new lines that show the percentage between line 1 and 2
, 2 and 3
and so on.
I've managed to do that but I'm not sure how I would manage to change the String.format specifically for those cells instead of the whole column.
CellTemplateSelector came to mind but I'm not sure that is a good idea as this might mean I have to set the binding again, not knowing the value of i
and such. Also I only want to change the string.format on the binding.
As I'm manipulating the number as a double (0,5 is 50%, 50 is 5000%) I guess I have to mask the input as well. not sure if String.Format does that for me as well or if I should use RadMaskedInput
Upvotes: 2
Views: 2897
Reputation: 102743
Use an IValueConverter
to convert the column's value at each row to its appropriate representation. So for the binding on a computed column, use:
// bind to the row data itself, not any specific property
Binding b = new Binding();
b.Converter = new RowToFormattedValueConverter
This will send the complete row data to the converter; so you should be able to either use some existing property of the row to figure out which type it is (regular or percentage), or add an extra hidden property/column to specify it explicitly. The converter would then look something like this:
public class RowToFormattedValueConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var rowData = (MyModelType)value;
string formattedResult;
if (rowData.IsPerecentageRow)
{
formattedResult= string.Format("{0:P1}", rowData.ValueIWant);
}
else
{
formattedResult= string.Format("{0:0.##}", rowData.ValueIWant);
}
return formattedResult;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Upvotes: 2