PDA

View Full Version : [CLOSED] Databind nested lists to gridpanel



Fahd
Feb 27, 2013, 2:31 PM
I am having some trouble binding some data to a dynamic gridpanel. The problem is that I have a list of records that contains another list. I want the outside list to be the rows of my table, while the values from the inside list are the columns. Do you have any suggestions on how to do this?

For example:
I have a list of trailers, and each has a list of stops.
I want the trailers to be the rows, and the names of each stop to be the columns, for instance:


TrailerName
Stop1
Stop2
Stop3


Trailer1
T1S1
T1S2
T1S3


Trailer2
T2S1
T2S2
T2S3


Trailer3
T3S1
T3S2
T3S3



Any ideas you have would be appreciated. Thank you.

Vladimir
Feb 27, 2013, 4:00 PM
Please demonstrate that data (how it is configured)
What exacly problems do you have? You can read list of stops and add fields and columns are based on that list

Fahd
Feb 27, 2013, 4:07 PM
Please demonstrate that data (how it is configured)
What exacly problems do you have? You can read list of stops and add fields and columns are based on that list

I am not sure how to bind the stops to the gridpanel. I can loop through the stops and add the appropriate number of columns, but I am not sure how to bind them afterwards. Ideally, I would like show the names of the stop as the column headers, and the arrivaltime of the each trailer at each of the stops (the stops would be the same for each trailer, but the times would be different).



public class Trailer
{
public List<Stop> Stops = new List<Stop>();
public string Name { get; set; }
}

public class Stop
{
public string Name { get; set; }
public DateTime ArrivalTime { get; set; }
}

Vladimir
Feb 27, 2013, 4:30 PM
Something like this


<%@ Page Language="C#" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
public class Trailer
{
public List<Stop> Stops = new List<Stop>();
public string Name { get; set; }
}


public class Stop
{
public string Name { get; set; }
public DateTime ArrivalTime { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
List<Trailer> trailers = new List<Trailer>
{
new Trailer{Name = "Trailer1", Stops = {
new Stop{Name = "T1S1"},
new Stop{Name = "T1S2"},
new Stop{Name = "T1S3"}
}},

new Trailer{Name = "Trailer2", Stops = {
new Stop{Name = "T2S1"},
new Stop{Name = "T2S2"},
new Stop{Name = "T2S3"}
}},

new Trailer{Name = "Trailer3", Stops = {
new Stop{Name = "T3S1"},
new Stop{Name = "T3S2"},
new Stop{Name = "T3S3"}
}}
};


Model model = new Model();
Store store = new Store
{
Model =
{
model
}
};


GridPanel grid = new GridPanel
{
Store = { store },
Width = 300,
Height = 150
};


List<object> data = new List<object>();
foreach (Trailer tr in trailers)
{
Dictionary<string, string> item = new Dictionary<string, string>();
item.Add("Trailer", tr.Name);
int i = 0;
foreach (Stop stop in tr.Stops)
{
item.Add("Stop" + ++i, stop.Name);
}


foreach (string key in item.Keys)
{
if (model.Fields.Get(key) == null)
{
model.Fields.Add(key);
grid.ColumnModel.Columns.Add(new Column { DataIndex = key, Text = key, Flex = 1 });
}
}

data.Add(item);
}


store.Data = data;


this.Form.Controls.Add(grid);
}
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server"/>

</form>
</body>
</html>

Fahd
Feb 27, 2013, 6:20 PM
Thank you, that works great!

Do you have a suggestion on how I can save the data? I.e. I want a save button at the bottom that will call a server-side method with the name-value pairs from each row. I am using a ComponentColumn to allow them to edit the records.

Vladimir
Feb 28, 2013, 4:05 AM
You can deserialize data to 'Dictionary<string, string>' like in the following sample
http://examples2.ext.net/#/GridPanel/Miscellaneous/Save_Filter/

After that transform dictionary to any required business logic object