PDA

View Full Version : [CLOSED] Dynamic model field and bind data



bayoglu
Apr 19, 2013, 12:05 PM
Hello,

For a graphics solution, I need to dynamically create store model fields and bind data to store. Name of the model fields are not static. I can easily create fields and add to store model in code behind like this:



ModelField mField = new ModelField();
mField.Type = ModelFieldType.Int;
mField.Name = "SName";
TrendModel.Fields.Add(mField);


The problem is that I am having difficulties binding data to the store. In C# I can not dynamically add properties to an already defined class. If it was possible I could enlarge the base class, fill it with data and bind to store data source. A sample scenario is as below:

Base class properties:
int ID;
DateTime Date;

Let's say dynamic fileds' names are A, B, and C. I need a data structure like this:

int ID;
DateTime Date;
int A;
int B;
int C;

A, B, C... are not known until the request is made. Depending on the selected values, I can configure store model to include A, B, C but don't know how to bind those values to store since I don't have an existing class for the structure. Whether I define it as list of objects (object[]), then again I can not dynamically set object property name like below:



new {
ID = 0,
Date = DateTime.Today,
This_property_dynamic_A = 1,
This_property_dynamic_B = 1,
This_property_dynamic_C = 1
},


The only way coming into my mind is building a json string with dynamic values and than serialize it to bind store. Is there another way to do it? Can you provide a sample for defining json string for such a dynamic scenario and then serialize it to bind store?

Thanks.

Daniil
Apr 19, 2013, 3:16 PM
Hi @bayoglu,

You can return metadata within a response of a load request.
http://examples2.ext.net/#/GridPanel/Data_Presentation/Meta_config/
http://examples2.ext.net/#/GridPanel/Data_Presentation/Meta_config_with_handler/

bayoglu
Apr 19, 2013, 3:50 PM
Hello Daniil,

Following this example (http://examples2.ext.net/#/GridPanel/Data_Presentation/Meta_config/) .

VS2010 says:



'Ext.Net.MetaConfig' does not contain a definition for 'IsRowEditing' and no extension method 'IsRowEditing' accepting a first argument of type 'Ext.Net.MetaConfig' could be found (are you missing a using directive or an assembly reference?)


for the below code:



meta.IsRowEditing = true;


Does it mean I have to update from SVN? (I am running v2.2.0.32327, updated on March 18).

Thanks.

Vladimir
Apr 19, 2013, 4:17 PM
You can use Dictionary and bind it via Data property of Store

bayoglu
Apr 19, 2013, 4:27 PM
You can use Dictionary and bind it via Data property of Store

Hi,
Could you provide a sample for using Dictionary object to bind store data?
Thanks.

Vladimir
Apr 19, 2013, 4:49 PM
Daniil already mentioned such example
http://examples2.ext.net/#/GridPanel/Data_Presentation/Meta_config/

Just you need a List of Dictionary


List<object> data = new List<object>();
data.Add(new Dictionary<string, string>{...});

Store1.Data = data;

bayoglu
Apr 19, 2013, 5:11 PM
I appreciate examples Daniil provided. They do not use dictionary object. Meanwhile, you mentioned to use dictionary object which sounds easier to use, so I asked for example.
Regarding the code lines you posted: Dictionary key is of type string and values are also string. Let's say dictionary key to be the name of the model field property (I guess), I have values of type string, int, bool etc. How would I represent such a structure with this dictionary definition? Does the {...} part have the magic?

If it is hard to provide a sample for dummies, never mind, close the threat.

Thanks anyway.

Vladimir
Apr 19, 2013, 5:34 PM
They do not use dictionary object.

Please see ReadData method, each data row is Dictionary



Dictionary<string, object> row = new Dictionary<string, object>();



Regarding the code lines you posted: Dictionary key is of type string and values are also string. Let's say dictionary key to be the name of the model field property (I guess), I have values of type string, int, bool etc. How would I represent such a structure with this dictionary definition? Does the {...} part have the magic?

You can use object for value to accept any values, values will be serialized correctly



If it is hard to provide a sample for dummies, never mind, close the threat.

It is not hard, just sample will not be different if compare with any other store samples except bound data. We already mentioned sample where Dictionary is used. If you have any troubles with Dictionary binding then please post a sample and we will be happy to assist you to resolve any issue

bayoglu
Apr 19, 2013, 5:56 PM
Sorry, I missed dictionary usage in the example... my bad. I did what I wanted, please close the threat.