如何在GridViewDataColumn中使用时将双NaN渲染停止为字符串

时间:2014-02-27 14:46:21

标签: .net wpf gridview telerik radgridview

我正在为(telerik)RadGridView动态创建列,我希望双NaN值显示为空字符串,目前它们显示为' NaN'。

有谁知道怎么做?

我希望有DataFormatString这个吗?

 var column = new GridViewDataColumn
 {
       Header = ExtractFriendlyName(member),
       DataType = valueType,
       DataMemberBinding = new Binding(member),
 };

通常我会使用FormatInfo类来执行此操作,例如:

var nfi = (NumberFormatInfo) NumberFormatInfo.CurrentInfo.Clone();
nfi.NaNSymbol = "NaN";

double num = double.NaN;
string numString = System.Number.FormatDouble(num, null, nfi);

1 个答案:

答案 0 :(得分:2)

这个简单的绑定转换器可以满足您的需求:

public class DoubleToStringConverter : IValueConverter
{
    public object Convert(
        object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is double)
        {
            double number = (double)value;
            value = double.IsNaN(number) ? string.Empty : number.ToString(culture);
        }

        return value;
    }

    public object ConvertBack(
        object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

...

DataMemberBinding = new Binding(member) { Converter = new DoubleToStringConverter() },