title: Adding Images in Row and Column in Grid
body: <p><img src="https://i.stack.imgur.com/m4l1i.png" alt="enter image description here"></p>
  
  <p>I have a problem where I am trying to add images to a grid, but the images are being added oddly. (See Picture). I am trying to add the images in a straight row, yet they are being added in the first spot then in the next row. I am a noob, so I am very confused. Thank you for your help.
  (The TUtils are just grabbing values from an .ini file)</p>
  
  <p>Code:</p>
  
  <pre><code>private void PopulateGrid()
  {
      Image img = CreateImage();
      ImageSize = TUtils.GetIniInt(Moleini, "ImageSize", "imageSize", 10);
      NumofImages= TUtils.GetIniInt(Moleini, "NumPictures", "pictures", 8);
      int ImageBorderSize = TUtils.GetIniInt(Moleini, "ImageBorder", "imageBorder", 2);
      double NumberOfColumns = TUtils.GetIniInt(Moleini, "NumRowsColumns", "columnNum", 4);
  
      // More Columns than Rows \\
      if (NumberOfColumns &gt; NumofImages)
      {
          MessageBox.Show("There is something wrong with the .ini file.");
          MainWin.Close();
      }
  
      // Math - Get Necessary Variables \\
      int ColumnSize = (ImageSize + (2 * ImageBorderSize));
      int RowSize = (ImageSize + (2 * ImageBorderSize));
      int NumberofRows = (int)Math.Ceiling(NumofImages / NumberOfColumns);
      int MainWindowWidth = (TUtils.ToInt(NumberOfColumns.ToString(), 2) * ColumnSize) + 15;
      int MainWindowHeight = (NumberofRows * RowSize) + 35;
  
      // Set Window Size \\
      MainWin.Width = MainWindowWidth;
      MainWin.Height = MainWindowHeight;
  
      // Create Grid \\
      MainWin.Content = grid_Main;
      grid_Main.Height = MainWindowHeight;
      grid_Main.Width = MainWindowWidth;
      grid_Main.Background = Brushes.Transparent;
  
      // Grid Properties \\
      for (int i = 0; i &lt; NumberOfColumns; i++)
      {
          ColumnDefinition newColumn = new ColumnDefinition();
          newColumn.Width = new GridLength(ColumnSize, GridUnitType.Pixel);
          grid_Main.ColumnDefinitions.Add(newColumn);
      }
  
      for (int i = 0; i &lt; NumberofRows; i++)
      {
          RowDefinition Row = new RowDefinition();
          Row.Height = new GridLength(RowSize, GridUnitType.Pixel);
          grid_Main.RowDefinitions.Add(Row);
      }
  
      // Fill Grid \\
      int RowCount = 0;
      int ColumnCount = 0;
      for (int i = 0; i &lt;= NumofImages; i++)
      {
          Image newImage = CreateImage();
          if (RowCount &lt; NumberofRows)
          {
              if (ColumnCount &lt; NumberOfColumns)
              {
                  Console.WriteLine("ColumnCount: " + ColumnCount.ToString());
                  Grid.SetRow(newImage, ColumnCount);
                  Grid.SetColumn(newImage, ColumnCount);
                  grid_Main.Children.Add(newImage);
                  ColumnCount++;
              }
  
              else
              {
                  RowCount++;
                  ColumnCount = 0;
                  Grid.SetRow(newImage, ColumnCount);
                  Grid.SetColumn(newImage, ColumnCount);
                  grid_Main.Children.Add(newImage);
                  ColumnCount++;
                  Console.WriteLine("RowCount: " + RowCount.ToString());
              }
          }
  
          else
          {
              break;
          }
  
  
      }
  }
  </code></pre>