View Full Version : Method to populate any store with any generic list

Oct 16, 2009, 1:48 PM

I didn't want to write a store object populating routine for every control that uses a Store object. So instead, I wrote a method which takes the store - as set in your aspx page - and a generic list of ANY type and populates the control specify in the StoreID tag. You could go one step further and dynamically create your store fields based upon the property names of the objects stored within the generic list but I couldn't get that to work. Hope someone find this useful

public static void BindStoreToDataControl<T>(Store store, List<T> list)
if (list != null || list.Count > 0)
Type t = typeof(T);
//make a new instance of the class name we figured out to get its props
object o = Activator.CreateInstance(t);
//gets all properties
PropertyInfo[] props = o.GetType().GetProperties();

Object[] dataContainer = new Object[list.Count];
int y = 0;
foreach (T item in list)
int x = 0;
Object[] data = new Object[props.Length];
//this acts as datacolumn
foreach (PropertyInfo pi in props)
//this is the row+col intersection (the value)
string propertyValue = Convert.ToString(item.GetType().GetProperty(pi.Nam e).GetValue(item, null));
data[x] = propertyValue;
dataContainer[y] = data;
store.DataSource = dataContainer;
store.DataSource = new Object[] { };

<ext:ComboBox ID="combobox1"

<ext:Store ID="store1" runat="server">
<ext:RecordField Name="ID" />
<ext:RecordField Name="Name" />

Oct 16, 2009, 1:52 PM

Why not just use JsonReader in Store and bind that List directly to the Store?

Oct 16, 2009, 1:55 PM
em em em.I didn't know you could - ignore me

Oct 16, 2009, 1:59 PM

All fine, it is good that you share code (and expirience). Don't hesitate to do it. I just asked additional info about your code to clear your idea

Oct 16, 2009, 2:04 PM
One of the things I tried to do was decouple the front end ext code from the database fieldnames. the code I posted was just short of that where the data populates the store but doesn't allow for the dynamic generation of recordfields for the store. If that could be done then I think - going to wear my bullet proof vest - it could help when someone renames a table field, then all they'll have to do is rename the property name as opposed to rename the ext code along with the database field mapping together with the object property name to keep eveything in sync.

So far I'm tying propertynames to database fields by using this

public long ID { get; set; }

public static string _ID = Helper.GetPropertyName((Centre s) => s.ID);

public static string GetPropertyName<T, TReturn>(Expression<Func<T, TReturn>> expression)
return ((MemberExpression)expression.Body).Member.Name;

and my dal layer starts building objects based upon dr[_ID] as opposed to dr["ID"] which gets very ugly. You could have constants which map to field names but I wanted to have my objects be very tightly coupled with the database filed names.

Still wearing my bullet proof vest and projectile shield.

Oct 17, 2009, 1:28 AM
Hi signup,

Thanks for sharing. I think this is a great idea.