Search code examples
c#winformslistcode-firstdatarepeater

How to display a list inside another list in a Winfoms DataRepeater


I am using C# Windows Forms and a codefirst database (Visual Studio 2013 Ultimate).

Is it possible to display a list inside another list in Windows Forms? (The emphasis is on -displaying- the data).

Please see this project as an example: https://postimg.cc/image/inunj8pxh/

I usually display a list with powerpacks´ datarepeater. For example when an order is placed by a customer, I can display the orderId, customerEmail, customerName etc. of the list of orders.

However, each order includes many different items. So far, I am not able to display any elements of the child-list (items) inside each element the datarepeater, where the parent-list (orders) is shown. The foreign-key of each item is the orderId, and the foreign-key of the order is the list of items (relationship order...items is 1..n).


Solution

  • I found the solution! It took me a while to figure out how to gain control over datarepeater items. Reading across many other forums and tutorials, I gradually worked my way through. Find in the screenshot my complete project: https://i.sstatic.net/jFa7G.png

    Any improvements in the code are more than welcome. Since I am quite new to the whole programming world, my code may not be optimized and the use of vocabulary may sometimes be inaccurate.

    Here you find the code of my Form1:

    namespace list_inside_list
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {

    } protected override void OnLoad(EventArgs e) { //First we load the list of Orders to datarepeater1 Program.CompanyContext _context = new Program.CompanyContext(); List<list_inside_list.Program.Order> listOrders = _context.Order.ToList(); program_OrderBindingSource1.DataSource = listOrders; //I don´t know why, but we need to load the list of items as well, although we never use the listItems variable List<list_inside_list.Program.Item> listItems = _context.Item.ToList(); /* * 1. We will loop through all the datarepater1 items (which is fed with listOrders) * 2. We assign currItem as datarepeater1.CurrentItem in order to "select" the current item at index j, * although we will never user currItem * 3. We tell the program that of the current datarepeater item we want use the current Order object * 4. We go through each of the currentOrder.items and print the itemName * */ DataRepeaterItem currItem = new DataRepeaterItem(); for (int j = 0; j < this.dataRepeater1.ItemCount; j++) { this.dataRepeater1.CurrentItemIndex = j; currItem = dataRepeater1.CurrentItem; var currentOrder = (list_inside_list.Program.Order)program_OrderBindingSource1.Current; foreach (var item in currentOrder.items) { dataRepeater1.CurrentItem.Controls["richTextBox1"].Text = dataRepeater1.CurrentItem.Controls["richTextBox1"].Text + item.itemName + "\n"; } } } }

    }