PDA

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



matrixwebtech
Dec 02, 2014, 6:32 PM
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>?

Daniil
Dec 03, 2014, 7:02 PM
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.

matrixwebtech
Dec 04, 2014, 3:32 AM
Hi daniil ,thanks for reply and also pointing me what I was doing wrong.please close this thread.