Answered by:
Windows Forms Gridview binding source from multiple collections

Question
-
I have 3 ObservableCollection<T1,T2,T3> collections where T represents 3 distinct implementations of an abstract class. A form manages adding objects to each of the three collections. I would like to display the string output from a common method from each of the combined 3 collections in a gridview control, for example they all implement ToString().
What is the optimal approach here, create a single event bound to each ObservableCollection<T1,T2,T3> which simply queries each collection and generates the output to clear/re-populate the gridview, or does a method exist to bind a datasource which itself is backed by 3 distinct collections? If the later is possible, I would imagine that is much more concise as I don't have to clear and regenerate the gridview.
Thanks!
- Edited by Ritmo2k Sunday, March 20, 2016 3:18 PM
- Moved by DotNet Wang Monday, March 21, 2016 2:12 AM
Sunday, March 20, 2016 3:16 PM
Answers
-
Hi Ritmo2k,
I couldn't clear your requirement, could you provide more detail information?
If you want to Stitching Data Source, you could refer to the following code(ObservableCollection + linq):
public abstract class T1 { public string ID1; public string Name1; public string ID2; } public abstract class T2 { public string ID2; public string Name2; public string ID3; } public abstract class T3 { public string ID3; public string Name3; } ObservableCollection<T1> obsT1 = new ObservableCollection<T1>(); ObservableCollection<T2> obsT2 = new ObservableCollection<T2>(); ObservableCollection<T3> obsT3 = new ObservableCollection<T3>(); private void Bind() { DataTable dt = new DataTable(); dt.Columns.Add("ID1"); dt.Columns.Add("Name1"); dt.Columns.Add("ID2"); dt.Columns.Add("Name2"); dt.Columns.Add("ID3"); dt.Columns.Add("Name3"); foreach (var t1 in obsT1) { DataRow dr = dt.NewRow(); dr["ID1"] = t1.ID1; dr["Name1"] = t1.Name1; dr["ID2"] = t1.ID2; T2 t2 = obsT2.Where(o => o.ID2 == t1.ID2).ToArray()[0]; if (t2 != null) { dr["Name2"] = t2.Name2; dr["ID3"] = t2.ID3; T3 t3 = obsT3.Where(o => o.ID3 == t2.ID3).ToArray()[0]; if (t3 != null) { dr["Name3"] = t3.Name3; } } dt.Rows.Add(dr); } } }
Regards,
Moonlight
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Proposed as answer by Moonlight ShengMicrosoft contingent staff Tuesday, March 29, 2016 5:07 AM
- Marked as answer by Moonlight ShengMicrosoft contingent staff Thursday, March 31, 2016 9:41 AM
Monday, March 21, 2016 9:53 AM
All replies
-
Hi Ritmo2k,
This forum is about the C# programming language, IDE, libraries, samples, and tools, as your issue is more related to the Windows Form development, we help you move it to the Windows Forms General forum for better support.
Thank you for your understanding.
Best Regards,
Albert Zhang
<p>We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.</p>
Monday, March 21, 2016 2:08 AM -
Hi Ritmo2k,
I couldn't clear your requirement, could you provide more detail information?
If you want to Stitching Data Source, you could refer to the following code(ObservableCollection + linq):
public abstract class T1 { public string ID1; public string Name1; public string ID2; } public abstract class T2 { public string ID2; public string Name2; public string ID3; } public abstract class T3 { public string ID3; public string Name3; } ObservableCollection<T1> obsT1 = new ObservableCollection<T1>(); ObservableCollection<T2> obsT2 = new ObservableCollection<T2>(); ObservableCollection<T3> obsT3 = new ObservableCollection<T3>(); private void Bind() { DataTable dt = new DataTable(); dt.Columns.Add("ID1"); dt.Columns.Add("Name1"); dt.Columns.Add("ID2"); dt.Columns.Add("Name2"); dt.Columns.Add("ID3"); dt.Columns.Add("Name3"); foreach (var t1 in obsT1) { DataRow dr = dt.NewRow(); dr["ID1"] = t1.ID1; dr["Name1"] = t1.Name1; dr["ID2"] = t1.ID2; T2 t2 = obsT2.Where(o => o.ID2 == t1.ID2).ToArray()[0]; if (t2 != null) { dr["Name2"] = t2.Name2; dr["ID3"] = t2.ID3; T3 t3 = obsT3.Where(o => o.ID3 == t2.ID3).ToArray()[0]; if (t3 != null) { dr["Name3"] = t3.Name3; } } dt.Rows.Add(dr); } } }
Regards,
Moonlight
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Proposed as answer by Moonlight ShengMicrosoft contingent staff Tuesday, March 29, 2016 5:07 AM
- Marked as answer by Moonlight ShengMicrosoft contingent staff Thursday, March 31, 2016 9:41 AM
Monday, March 21, 2016 9:53 AM