Reputation: 1844
I have two list of different objects :
List<Report>
List<Newsletter>
each having a 'created date' property. I need to sort the created date of both lists in descending order and bind it to a gridview.
How can this be done, as i can provide only one datasource?
Upvotes: 0
Views: 2844
Reputation: 943
If both types have the same properties you want displayed, then you could create an interface that they both implement. Then your view can have the following.
PseudoCode:
List<IDisplayReportsAndNewsletter> data = new List<IDisplayReportsAndNewsletter>();
data.Addrange(reports);
data.Addrange(newsletters);
grid.Datasource = data.OrderBy(d => d.CreatedDate).ToList();
where reports and newsletters are the lists you describe above where the classes now implement your new IDisplayReportsAndNewsletter interface.
The main benefit you get from this over the dataset option (which is also valid) is that you can still work with your domain objects in the view as opposed to having to translate to and from dataset rows.
The downside is that you pollute your domain objects with an interface just for the purpose of your view
Upvotes: 0
Reputation: 12786
You could firstly create List
of dates:
var rows = reports.Select(x => x.CreateDate).Union(news.Select(x => x.CreateDate)).OrderByDescending(x => x.Date).ToList();
And then use a DataTable
structure for binding:
DataTable dt = new DataTable();
dt.Columns.Add("Date");
foreach (var row in rows) {
dt.Rows.Add(row);
}
grid.DataSource = dt;
Upvotes: 3