0

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).

philipp-fx
  • 619
  • 1
  • 8
  • 21
  • 1
    An image would help to understand what you are looking for. Also: your choice of tags seems somewhat confusing.. Treeviews contain nested lists. Other than that you need to code it yourself. Here is a coded [accordion example](http://stackoverflow.com/questions/29005397/accordion-in-windows-forms-datagridview/29006361?s=1|0.4533#29006361) – TaW Aug 09 '16 at 20:54
  • I quickly created a new project, because my real project contains sensitive data. Please check this image for a complete overview of my project and my visualized goal. https://postimg.org/image/inunj8pxh/ – philipp-fx Aug 10 '16 at 11:50
  • Hm, this lokks as if you should go for nested flowlayoutpanels and usercontrols..you may still want ot use an activation/expansion mechanism like in the accordion example, but it doesn't look like a tight grid of rows; so a DGV doesn't seem called for.. In case you need it, don't forget to think ahead about databinding when doing the layout.. – TaW Aug 10 '16 at 13:42
  • Thank you for your time and your input TaW! So there is no easier way, for example staying with datarepeater and adding a line of code to bind the list of items to the listBox1 for each orderId? (I only want to display it, and nothing else.) Meanwhile, I will read into nested flowlayoutpanels and usercontrols, since I don´t know anything about these topics yet. Again, thanks a lot!! – philipp-fx Aug 10 '16 at 14:11
  • I don't know datarepeater. I was under the impression you want the nested list displayed __inside__ an element of the outer list. If that is not the goal you have basically a master/detail situation and could use any list control. (ListBox is a dumb one, ListView is much more flexible, if you need more layout than just a line of text..) – TaW Aug 10 '16 at 14:19
  • Looking up datarepeater I should say that I'm not really sure of the things I wrote. maybe there is a better way with it..But folks who use it need to jump in here.. I have edited the Title&Tags to make that a little more likely.. – TaW Aug 10 '16 at 14:23

1 Answers1

0

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.stack.imgur.com/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";
            }

        }
    }
}

}

philipp-fx
  • 619
  • 1
  • 8
  • 21