[CLOSED] store.LoadData not working when data source is List<Object>

  1. #1

    [CLOSED] store.LoadData not working when data source is List<Object>

    Hi
    I am trying to load store data with LoadData () function of Store.I try to explain what steps I follow

    Sample Controller

    public class ListOfObjectController : Controller
        {
            //
            // GET: /ListOfObject/
    
            public ActionResult Index()
            {
                return View();
            }
    
    
            public DirectResult LoadStore()
            {
                List<object> o = new List<object>();
    
                o = Function(1, 2);
    
                var store = X.GetCmp<Store>("Store1");
    
                store.LoadData(o);
                return this.Direct();
            }
    
            public List<object> Function(Int64 p1, Int64 p2)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("DBFIELD1",typeof(System.String));
                dt.Columns.Add("DBFIELD2", typeof(System.Int32));
                dt.Columns.Add("DBFIELD3", typeof(System.Int64));
    
                for (int i = 0; i <= 2; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["DBFIELD1"] = i.ToString();
                    dr["DBFIELD2"] = i;
                    dr["DBFIELD3"] = Convert.ToInt64(i);
                    dt.Rows.InsertAt(dr, dt.Rows.Count);
                }
    
                /*
                execute database query and populate datatable
                */
    
                var list_of_Object = new List<Object>(dt.Rows.Count);
    
                foreach (DataRow row in dt.Rows)
                {
                    var values = row;
                    var _temp = new[]
                    {
                        new
                        {
                        DBFIELD1 = values[0].ToString(),
                        DBFIELD2= Convert.ToInt32(values[1]),
                        DBFIELD3= Convert.ToInt64(values[2]),
                        
                         
                        }
                    };
                    list_of_Object.Add(_temp);
                }
                return list_of_Object.ToList();
    
    
            }
    
        }
    Sample View
    @X.ResourceManager()
            @(X.GridPanel()
            .Store(
    Html.X().Store()
    .ID("Store1")
    .AutoLoad(false)
    .Model(
        Html.X().Model()
            .Name("Model1")
             .IDProperty("DBFIELD1 ")
            .Fields(
     
                        Html.X().ModelField().Name("DBFIELD1").Type(ModelFieldType.String).ServerMapping("DBFIELD1"),
                        Html.X().ModelField().Name("DBFIELD2").Type(ModelFieldType.Int).ServerMapping("DBFIELD2"),
                        Html.X().ModelField().Name("DBFIELD3").Type(ModelFieldType.Int).ServerMapping("DBFIELD3")
                        )
                        )
                        )
                        .ColumnModel
                        (
                        X.Column()
                        .DataIndex("DBFIELD1")
                        .Text("DBFIELD1")
                        ,
                         X.Column()
                        .DataIndex("DBFIELD2")
                        .Text("DBFIELD2")
                        ,
                         X.Column()
                        .DataIndex("DBFIELD3")
                        .Text("DBFIELD3")
                        
                        )
                        .TopBarItem
                        (
                            X.Button().Text("Click") 
                            .DirectClickUrl("ListOfObject/LoadStore")
                            
                        )
                        
                        )
    Store not load with above process,but if i create a class and then prepare a list of this class follow above process then its works fine.then problem is if I start creating individual class for different data representation its going to unmanageable and also time consuming.

    Working Code With List of Class

    public class ListOfObjectController : Controller
        {
            //
            // GET: /ListOfObject/
    
            public ActionResult Index()
            {
                return View();
            }
    
    
            public DirectResult LoadStore()
            {
                List<testobjectlist> o = new List<testobjectlist>();
    
                o = Function(1, 2);
    
                var store = X.GetCmp<Store>("Store1");
    
                store.LoadData(o);
                return this.Direct();
            }
            public List<testobjectlist> Function(Int64 p1, Int64 p2)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("DBFIELD1", typeof(System.String));
                dt.Columns.Add("DBFIELD2", typeof(System.Int32));
                dt.Columns.Add("DBFIELD3", typeof(System.Int64));
    
                for (int i = 0; i <= 2; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["DBFIELD1"] = i.ToString();
                    dr["DBFIELD2"] = i;
                    dr["DBFIELD3"] = Convert.ToInt64(i);
                    dt.Rows.InsertAt(dr, dt.Rows.Count);
                }
    
                /*
                execute database query and populate datatable
                */
    
                var list_of_Object = new List<testobjectlist>(dt.Rows.Count);
    
                foreach (DataRow row in dt.Rows)
                {
                    var values = row;
                    var _temp = new testobjectlist
                    {
    
                        DBFIELD1 = values[0].ToString(),
                        DBFIELD2 = Convert.ToInt32(values[1]),
                        DBFIELD3 = Convert.ToInt64(values[2]),
    
    
    
                    };
                    list_of_Object.Add(_temp);
                }
                return list_of_Object.ToList();
    
    
            }
    
            
    
        }
    
    
        public class testobjectlist
        {
            public string DBFIELD1 { get; set; }
            public Int32 DBFIELD2 { get; set; }
            public Int64 DBFIELD3 { get; set; }
        }
    is there any way to do this with List<Object>?
    Last edited by Daniil; Dec 04, 2014 at 6:35 AM. Reason: [CLOSED]
  2. #2
    Hi @matrixwebtech,

    There is an essential difference between
    var _temp = new[]
    {
        new
        {
            DBFIELD1 = values[0].ToString(),
            DBFIELD2= Convert.ToInt32(values[1]),
            DBFIELD3= Convert.ToInt64(values[2])
        }
    };
    and
    var _temp = new testobjectlist
    {
        DBFIELD1 = values[0].ToString(),
        DBFIELD2 = Convert.ToInt32(values[1]),
        DBFIELD3 = Convert.ToInt64(values[2])
    };
    The first code snippet creates an array of object, the second one creates just an object.

    You should not create an array of objects here.
  3. #3
    Hi daniil ,thanks for reply and also pointing me what I was doing wrong.please close this thread.

Similar Threads

  1. [CLOSED] Add a object List in Store
    By osef in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Nov 25, 2013, 7:52 PM
  2. [CLOSED] Convert Data from Store to object data list
    By Zenalyse in forum 2.x Legacy Premium Help
    Replies: 3
    Last Post: Apr 30, 2013, 6:13 AM
  3. Delete from Grid with Object Data Source attached
    By ambruslaco in forum 1.x Help
    Replies: 3
    Last Post: Dec 13, 2012, 7:17 AM
  4. Replies: 0
    Last Post: Feb 05, 2012, 12:33 AM
  5. Store original object from data source
    By nextSTEP in forum 1.x Help
    Replies: 8
    Last Post: Feb 16, 2011, 1:12 PM

Posting Permissions