title: C# Create group boxes dynamically depending on data received in Windows Forms
body: <p>I am currently working on windows forms application, and I want to dynamically create form, which creates GroupBox depending on categories and products. 
  I have two tables like:</p>
  
  <pre><code>+-------------------------------+
  |           Category            |
  +-------------------------------+
  | CategoryId (UNIQUEIDENTIFIER) |
  | Name (VARCHAR(255))           |
  | CreatedBy(VARCHAR(255))       |
  | CreationDate(DATETIME)        |
  +-------------------------------+
  
  
  +-----------------------------+
  |           Product           |
  +-----------------------------+
  | ProductId(UNIQUEIDENTIFIER) |
  | CategoryId (FK)             |
  | Name(VARCHAR(255))          |
  | CreatedBy(VARCHAR(255))     |
  | CreationDate(DATETIME)      |
  | IsDeleted(BOOL)             |
  +-----------------------------+
  </code></pre>
  
  <p>For example if I have category <code>Fruit</code> and its products are <code>Banana</code> and <code>Apple</code>, but I have another category called <code>Vegetables</code> and its products are <code>Avocado</code> and <code>Tomato</code> like:</p>
  
  <pre><code>+--------------------------------------+---------------------+-------------------------+------------------------+
  |    CategoryId (UNIQUEIDENTIFIER)     | Name (VARCHAR(255)) | CreatedBy(VARCHAR(255)) | CreationDate(DATETIME) |
  +--------------------------------------+---------------------+-------------------------+------------------------+
  | CEF50872-B8E7-4EA0-A3D7-00048FFC82DA | Fruit               | Test                    | 10/11/2018             |
  | 646B5A64-EA28-4471-8964-0017EB511797 | Vegetables          | Test                    | 10/12/2018             |
  +--------------------------------------+---------------------+-------------------------+------------------------+
  
  
  +--------------------------------------+--------------------------------------+---------+-------------------------+------------------------+
  |     ProductId(UNIQUEIDENTIFIER)      |           CategoryId (FK)            |  Name   | CreatedBy(VARCHAR(255)) | CreationDate(DATETIME) |
  +--------------------------------------+--------------------------------------+---------+-------------------------+------------------------+
  | D81EFF8C-9B8D-48C9-9CC7-0015B2787D4A | CEF50872-B8E7-4EA0-A3D7-00048FFC82DA | Banana  | Test                    | 10/11/2018             |
  | 7683554C-5E46-40FE-9285-001E8CD67740 | CEF50872-B8E7-4EA0-A3D7-00048FFC82DA | Apple   | Test                    | 10/11/2018             |
  | 614FBE96-6355-4C3B-985B-002E2B9853CB | 646B5A64-EA28-4471-8964-0017EB511797 | Avocado | Test                    | 10/11/2018             |
  | 74352B1B-36E8-4913-898E-002ED4CB21AD | 646B5A64-EA28-4471-8964-0017EB511797 | Tomato  | Test                    | 10/11/2018             |
  +--------------------------------------+--------------------------------------+---------+-------------------------+------------------------+
  </code></pre>
  
  <p>When I receive that info in <code>c#</code> from stored procedure like </p>
  
  <pre><code>SELECT * FROM Category AS c
  INNER JOIN Product AS P ON C.CategoryId = P.CategoryId
  </code></pre>
  
  <p>So, I want to create <code>groupbox</code> dynamically. How can I create this <code>groupbox</code> depending on categories and their products? Regards</p>
  
  <p>At the final I want something like this:
  <a href="https://i.stack.imgur.com/fyiLV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fyiLV.png" alt="enter image description here"></a></p>
  
  <p><strong>Update</strong></p>
  
  <p>I try as comments bellow like:</p>
  
  <p>Receive data from datatable:</p>
  
  <pre><code>  var data = db.GetTableBySQL("usp_RedMarks_Get");
  
      foreach (DataRow itm in data.Rows)
      {
          if (itm["CategoryName"].ToString() != CurrentGroupBoxName)
          {
              flpRedMarks.Controls.Add(GetGroupBox(itm, 200, 100));
          }
      }
  </code></pre>
  
  <p>Create GroupBox and items:</p>
  
  <pre><code> private GroupBox GetGroupBox(DataRow c, int width, int height)
          {
              GroupBox currentGroupBox = new GroupBox();
              currentGroupBox.Size = new Size(width, height);
              currentGroupBox.Text = c["CategoryName"].ToString();
              currentGroupBox.Name = c["CategoryName"].ToString();
              CurrentGroupBoxName = currentGroupBox.Name;
  
              var y = 20;
              foreach (var itm in c.Table.Rows)
              {
                  CheckBox cb = new CheckBox();
                  cb.Text = itm.ToString();
                  cb.Location = new Point(5, y);
                  // you can add an event here...
                  //cb.CheckedChanged += cb_SomeEvent;
                  currentGroupBox.Controls.Add(cb);
                  y += 20;
              }
  
              return currentGroupBox;
          }
  </code></pre>
  
  <p>I receive data from SQL like:</p>
  
  <p><a href="https://i.stack.imgur.com/qWlwg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qWlwg.png" alt="enter image description here"></a></p>
  
  <p>But instead of get items(products) based in categories I get merged data:
  <a href="https://i.stack.imgur.com/7jqXr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7jqXr.png" alt="enter image description here"></a></p>
  
  <p>What am I doing wrong? Regards</p>