title: How to Programmatically create Grid within row of a Grid in Wpf
body: <p>I am working in WPF -- There is <code>button</code> with <code>click event handler</code> in my application. As i click on button it's event handler generates a new row in grid named as <code>grids</code>. In this new Row i want to add another grid programmatically to add Label, Button and TextBox in this grid in row.
  As i executed my code it only generates a texboxes! where labels and button shown once! Here code and image is : <em>Please feel free to ask if my query is not clear to you!</em></p>
  
  <pre><code> int r =0;
   private void button2_Click(object sender, RoutedEventArgs e)
      {
          TextEdit text1; Button button1; Grid grid1;
          grids.RowDefinitions.Add(new RowDefinition());
          text1 = new TextEdit();
          text1.SetValue(Grid.ColumnProperty, 1);
          text1.SetValue(Grid.RowProperty, r);
          button1 = new Button();
          button1.Content = "Left + " + r;
          button1.Click += new RoutedEventHandler(button1_Click);
          button1.SetValue(Grid.ColumnProperty, 1);
          button1.SetValue(Grid.RowProperty, r);
          grid1 = new Grid();
          grid1.SetValue(Grid.ColumnProperty, 1);
          grids.RowDefinitions.Add(new RowDefinition());
          grid1.SetValue(Grid.RowProperty, r);
          grids.Children.Add(button1);
          grids.Children.Add(text1);
          r = r + 1;
      }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/2oz4I.png" alt="enter image description here">
  <strong>EDIT</strong></p>
  
  <pre><code> int r =0;
   private void button2_Click(object sender, RoutedEventArgs e)
    {
      db obj = new db();
      var query = from p in obj.TableA select p ;
  
    foreach(var a in query.ToList())
    {
      TextEdit text1; Button button1; Grid grid1;
      grids.RowDefinitions.Add(new RowDefinition());
      text1 = new TextEdit();
      text1.SetValue(Grid.ColumnProperty, 1);
      text1.SetValue(Grid.RowProperty, r);
      button1 = new Button();
      button1.Content = a.name;
      button1.Click += new RoutedEventHandler(button1_Click);
      button1.SetValue(Grid.ColumnProperty, 1);
      button1.SetValue(Grid.RowProperty, r);
      grid1 = new Grid();
      grid1.SetValue(Grid.ColumnProperty, 1);
      grids.RowDefinitions.Add(new RowDefinition());
      grid1.SetValue(Grid.RowProperty, r);
      grids.Children.Add(button1);
      grids.Children.Add(text1);
      r = r + 1;}
  }
  </code></pre>