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

RadDataForm with expandoObject

3 Answers 42 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
Vivien
Top achievements
Rank 1
Vivien asked on 06 Jul 2016, 05:26 PM

Hello,

I want to display a DataForm for enter a new element in a grid. But this grid is generated dynamically.

For that, i use a: "dynamic data = new ExpandoObject()" and feed that with: "data.field1 = "string"; data.field2 = 10" and bind my DataForm on it: "<telerik:RadDataForm x:Name="RadDataForm" AutoGenerateFields="True"
                                     AutoCommit="True" EditEnded="DataForm_OnEditEnded"
                                     AutoEdit="True" CommandButtonsVisibility="Commit,Cancel"
                                     CurrentItem="{Binding Path=DataContext.data, RelativeSource={RelativeSource Self}}"
                                     Header="Hello world"
                                     KeyboardNavigation.TabNavigation="Cycle">
</telerik:RadDataForm>"

My problem now, the DataForm change my Int type on string type...

Have you some idea?

3 Answers, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 11 Jul 2016, 10:40 AM
Hello Vivien,

The reason you're observing this behavior is because the ExpandoObject's values are of type object. You could, however, handle RadDataForm's AutoGeneratingField event and process the values there. Here's an example, following the approach described in this article:

private void RadDataForm_AutoGeneratingField(object sender, Telerik.Windows.Controls.Data.DataForm.AutoGeneratingFieldEventArgs e)
{
    var property = (RadDataForm.CurrentItem as IDictionary<string, object>)[e.PropertyName];
    if (IsNumericType(property.GetType()))
    {
        var binding = e.DataField.DataMemberBinding;
        binding.Converter = new IntToNullableDoubleConverter();
        e.DataField = new DataFormNumericUpDownField() { Label = e.DataField.Label, DataMemberBinding = binding };
    }
}
 
public static bool IsNumericType(Type type)
{
    TypeCode typeCode = Type.GetTypeCode(type);
    //The TypeCode of numerical types are between SByte (5) and Decimal (15).
    return (int)typeCode >= 5 && (int)typeCode <= 15;
}

I hope you find this helpful.

Regards,
Dilyan Traykov
Telerik by Progress
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Vivien
Top achievements
Rank 1
answered on 13 Jul 2016, 06:44 PM
Thanks you, I try that, it fell good :)
0
Vivien
Top achievements
Rank 1
answered on 28 Jul 2016, 07:47 PM

Thanks :)

 

Tags
DataForm
Asked by
Vivien
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Vivien
Top achievements
Rank 1
Share this question
or