1

Here I have these Classes:

public class CustomerDebt
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Family { get; set; }
    public string Remain { get; set; }
    public List<Shops> details = new List<Shops>();
}

public class Shops
{
    public List<int> ints = new List<int>();
    public List<string> strings = new List<string>();
}

I'm making a list of CustomerDebts objects:

List<CustomerDebt> debts = new List<CustomerDebt>();

and Here I will add one item to it:

Shops shop = new Shops();
shop.ints.Add(1);
shop.ints.Add(2);

shop.strings.Add("M");
shop.strings.Add("S");
shop.strings.Add("F");

CustomerDebt d = new CustomerDebt();

d.ID = 12;
d.Name = "Joe";
d.Family = "Steven";
d.Remain = "1000";
d.details.Add(shop);

debts.Add(d);

Now I bind debts list into a grid view:

gridview.DataSource = debts;

It will automatically fill the grid view with only "ID,Name,Family,Remain" properties of CustomerDebt Class. But I need to bind the List property which is a list of another Class into grid view.

I think it needs something like {get; set;} to be shown in grid, But also I do not know If the grid can show a nested class like this.

Hope I'm clear. What should I do to get a clean Grid view with such list of Class objects?

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
Inside Man
  • 4,194
  • 12
  • 59
  • 119
  • I don't recall that the .NET Build-In Windows Forms Grid Supports Master-Detail DataSources. And Yes when the controls supports that, you need a public getter `public List details { set; get; } = new List();` – Zein Makki Aug 30 '16 at 11:20

2 Answers2

2

There is no built-in support for Master Details in the DataGridView Control in Windows Forms. But we have the option to use Third Party Libraries Like:


As for the binding issue, yes you need to have a public property with a public getter:

public List<Shops> details { set; get; } = new List<Shops>();

For prior to C# 6 you can use:

public class CustomerDebt
{
    public CustomerDebt()
    {
       details = new List<Shops>();
    }

    public List<Shops> details { set; get; }
}
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • I'm using Telerik Grid, But about the public getter, it won't work like you wrote and will give ERROR! – Inside Man Aug 30 '16 at 11:29
  • @Stranger This is a C# 6 syntax. Check the edit for prior versions. – Zein Makki Aug 30 '16 at 11:31
  • Two questions. First How can I find out my C# version? i'm using visual studio 2013, Second, it seems it is working now, But Telerik Grid can not show the details column with is inner data which are two another lists. I will just write a text in it like "System.Collection.Generic" – Inside Man Aug 30 '16 at 11:35
  • @Stranger you need to do the same for the Lists inside the Shop Class. However, I'm not certain that Telerik's Grid Supports multiple levels of nesting. As for c# version, check this : http://stackoverflow.com/a/19532977/3185569 – Zein Makki Aug 30 '16 at 11:38
  • Yes I did it with Lists inside the Shop Class but Telerik can not support such view. what should I do? – Inside Man Aug 30 '16 at 11:42
  • @Stranger You need to find a workaround to display all the IDs in on line. Maybe Comma delimited. If that is acceptable in the business logic. – Zein Makki Aug 30 '16 at 11:44
  • @Stranger Actually, check this it might help: http://www.telerik.com/forums/radgrid-with-multiple-nested-grids-and-custom-paging .. I have no further information on this topic. – Zein Makki Aug 30 '16 at 11:45
1

Another way of doing this other than the accepted answer can be like below. Columns can be added manually and then the rows can be filled accordingly.

        this.gridview.Columns.Add("ID", "ID");
        this.gridview.Columns.Add("Name", "Name");
        this.gridview.Columns.Add("Family", "Family");
        this.gridview.Columns.Add("Remain", "Remain");
        this.gridview.Columns.Add("Details", "Details");

        foreach (var debt in debts)
           foreach (var detail in debt.details)
               this.dataGridView1.Rows.Add(debt.ID, debt.Name, debt.Family, debt.Remain, "Ints:" + string.Join(",", detail.ints) + "   Strings:" + string.Join(",", detail.strings));
Omer Kahoot
  • 144
  • 1
  • 9