Method to populate any store with any generic list

  1. #1

    Method to populate any store with any generic list

    Hi

    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.Name).GetValue(item, null));
                        data[x] = propertyValue;
                        x++;
                    }
                    dataContainer[y] = data;
                    y++;
                }
                store.DataSource = dataContainer;
                store.DataBind();
            }
            else
            {
                store.DataSource = new Object[] { };
                store.DataBind();
            }
        }
    
    <ext:ComboBox ID="combobox1"
                        StoreID="store1" 
                        FieldLabel="Test" 
                        runat="server" 
                        Editable="false" 
                        ReadOnly="true" 
                        DisplayField="Name" 
                        ValueField="ID" 
                        Width="250">
                        </ext:ComboBox> 
    
    
    <ext:Store ID="store1" runat="server">   
        <Reader>
            <ext:ArrayReader>
                <Fields>
                    <ext:RecordField Name="ID" />
                    <ext:RecordField Name="Name" />
                </Fields>
            </ext:ArrayReader>
        </Reader>          
    </ext:Store>
  2. #2

    RE: Method to populate any store with any generic list

    Hi,

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

    RE: Method to populate any store with any generic list

    em em em.I didn't know you could - ignore me
  4. #4

    RE: Method to populate any store with any generic list

    Hi,

    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
    :)
  5. #5

    RE: Method to populate any store with any generic list

    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.
  6. #6

    RE: Method to populate any store with any generic list

    Hi signup,

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




    Geoffrey McGill
    Founder

Similar Threads

  1. Replies: 0
    Last Post: Jul 30, 2012, 3:32 PM
  2. Populate HTML select with Store
    By Tbaseflug in forum 1.x Help
    Replies: 1
    Last Post: Nov 06, 2011, 3:36 PM
  3. Replies: 0
    Last Post: Jan 04, 2011, 8:25 PM
  4. Creating a Generic Store
    By Fredrik in forum Examples and Extras
    Replies: 2
    Last Post: Nov 29, 2010, 5:30 PM
  5. [CLOSED] Populate store from xml file
    By 78fede78 in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Oct 04, 2010, 11:11 AM

Posting Permissions