Hi,
I'm developing with VS 2012 and Telerik control version 2013.2.724.40.
I tried to bind string values dynamically to the GridView in code.
The program first reads from a array, where it can decide the type of the column(e.g. string, double). If the data can be presented as a double value, then I create a customized GridView column with RadMaskedTextInput. I'd like to format the number by setting the Text property. The data binding works fine, however, it seems the numbers (e.g. 1000.1) are not shown as expected (e.g. 1,000.10).
I could attach all source code if needed. Thanks in advance.
Regards,
Gong
I'm developing with VS 2012 and Telerik control version 2013.2.724.40.
I tried to bind string values dynamically to the GridView in code.
The program first reads from a array, where it can decide the type of the column(e.g. string, double). If the data can be presented as a double value, then I create a customized GridView column with RadMaskedTextInput. I'd like to format the number by setting the Text property. The data binding works fine, however, it seems the numbers (e.g. 1000.1) are not shown as expected (e.g. 1,000.10).
I could attach all source code if needed. Thanks in advance.
Regards,
Gong
public
MainWindow()
{
InitializeComponent();
IList<DataHeader> headerList = DataService.Instance.HeaderList;
for
(
int
i = 0; i < headerList.Count; i++)
{
DataHeader curentHeader = headerList[i];
GridViewBoundColumnBase column;
if
(curentHeader.Name ==
"AMOUNT"
)
{
column =
new
CGridViewNumColumn();
}
else
{
column =
new
GridViewDataColumn();
}
column.DataMemberBinding =
new
Binding(
"DataItemList["
+ i +
"]"
)
{
UpdateSourceTrigger = UpdateSourceTrigger.LostFocus
};
column.Header = curentHeader.Caption;
column.UniqueName = curentHeader.Name;
GridControl.Columns.Add(column);
}
DataContext = DataService.Instance.Data;
}
internal
class
CGridViewNumColumn : GridViewBoundColumnBase
{
public
override
FrameworkElement CreateCellEditElement(GridViewCell cell,
object
dataItem)
{
BindingTarget = RadMaskedTextInput.ValueProperty;
var input =
new
RadMaskedTextInput
{
HorizontalContentAlignment = HorizontalAlignment.Right,
Culture = CultureInfo.GetCultureInfo(
"en-GB"
),
FormatString =
"n2"
,
Mask =
""
,
MinWidth = 100,
SelectionOnFocus = SelectionOnFocus.SelectAll
};
input.SetBinding(BindingTarget, CreateValueBinding());
input.LostFocus += InputOnLostFocus;
return
input;
}
private
void
InputOnLostFocus(
object
sender, RoutedEventArgs routedEventArgs)
{
var input = sender
as
RadMaskedTextInput;
if
(input ==
null
)
throw
new
ArgumentNullException(
"sender"
);
if
(
string
.IsNullOrWhiteSpace(input.Value))
{
input.Value =
string
.Empty;
return
;
}
double
value;
//Format the number for frontend.
const
NumberStyles style = NumberStyles.Float | NumberStyles.AllowThousands;
if
(!
double
.TryParse(input.Value, style, input.Culture,
out
value))
{
input.Value =
"0"
;
}
input.Text = value.ToString(input.FormatString, input.Culture);
//Format the number for back-end. Extra precisions will be removed.
if
(!
double
.TryParse(input.Text, style, input.Culture,
out
value))
{
input.Value =
"0"
;
}
input.Value = value.ToString(input.Culture);
}
private
Binding CreateValueBinding()
{
var valueBinding =
new
Binding
{
Mode = BindingMode.TwoWay,
NotifyOnValidationError =
true
,
ValidatesOnExceptions =
true
,
Path =
new
PropertyPath(DataMemberBinding.Path.Path)
};
return
valueBinding;
}
}