title: Add DataGrid.Columns from code behind in WPF
body: <p>I have a <code>DataGrid</code> with data bind from code behind also I have 2 columns for Edit and delete like this </p>
  
  <pre><code>&lt;DataGrid Height="226" HorizontalAlignment="Left" Margin="106,111,0,0" Name="DgEmp" VerticalAlignment="Top" Width="684" ColumnWidth="*" CanUserAddRows="False"&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTemplateColumn Header="Actions"&gt;
              &lt;DataGridTemplateColumn.CellTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;StackPanel Orientation="Horizontal"&gt;
                          &lt;Button Content="Delete" Name="BtnDelete" Click="btnDelete_Click"/&gt;
                          &lt;Button Content="Edit" x:Name="BtnEdit" Click="btnEdit_Click"/&gt;
                      &lt;/StackPanel&gt;
                  &lt;/DataTemplate&gt;
              &lt;/DataGridTemplateColumn.CellTemplate&gt;
          &lt;/DataGridTemplateColumn&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>It add buttons in the 1st two columns. <strong>How can I add this from code behind</strong>.</p>
  
  <p>Also I have another confusion I bind my <code>DataGrid</code> using the below method</p>
  
  <pre><code>private void GridBind()
  {
      try
      {
          using (var con = new SqlConnection(_conString))
          {
              const string sqlqry = "select ROW_NUMBER() OVER (ORDER BY EmpNo) AS [SlNo],EmpNo,EmpName,Salary,DeptNo from dbo.Employee";
              var cmd = new SqlCommand(sqlqry, con);
              _adapt = new SqlDataAdapter(cmd);
              var dt = new DataTable("Employee");
              _adapt.Fill(dt);
              DgEmp.ItemsSource = dt.DefaultView;
          }
      }
      catch (Exception ex)
      {
          MessageBox.Show("Some error encountered" + ex.Message);
      }
  }
  </code></pre>
  
  <p><strong>This Binding method is not working with the following</strong> <code>XAML</code></p>
  
  <pre><code>&lt;DataGrid Height="226" HorizontalAlignment="Left" Margin="106,111,0,0" Name="DgEmp" VerticalAlignment="Top" Width="684" ColumnWidth="*" CanUserAddRows="False"&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn Header="SL No" Width="*"/&gt; 
          &lt;DataGridTextColumn Header="Emp No" Width="*"/&gt;
          &lt;DataGridTextColumn Header="Emp Name" Width="*" /&gt;
          &lt;DataGridTextColumn Header="Salary" Width="*" /&gt;
          &lt;DataGridTextColumn Header="Dept No" Width="*" /&gt;
          &lt;DataGridTemplateColumn Header="Actions"&gt;
              &lt;DataGridTemplateColumn.CellTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;StackPanel Orientation="Horizontal"&gt;
                          &lt;Button Content="Delete" Name="BtnDelete" Click="btnDelete_Click"/&gt;
                          &lt;Button Content="Edit" x:Name="BtnEdit" Click="btnEdit_Click"/&gt;
                      &lt;/StackPanel&gt;
                  &lt;/DataTemplate&gt;
              &lt;/DataGridTemplateColumn.CellTemplate&gt;
          &lt;/DataGridTemplateColumn&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>And the last when I am trying delete record on its <code>clickevent</code> like WinForm its not working</p>
  
  <pre><code> private void btnDelete_Click(object sender, RoutedEventArgs e)
   {
      var id= Convert.ToInt32(DgEmp.Rows[e.RowIndex].Cells[2].Value.ToString()); 
      if(id!=0)
      {
           // Delete query on basis of id
           GridBind();
           ClearData();
       }
  }
  </code></pre>
  
  <p>Sorry for asking too many question at once as these are the problems I am facing shifting from <code>WinForm</code> to <code>WPF</code>.</p>
  
  <p><strong>EDIT : I have tried this:</strong>
  What I have added is</p>
  
  <pre><code>private void Window_Loaded(object sender, RoutedEventArgs e)
          {
              GridBind();
              string colProperty = "Name";
              DataGridTextColumn col = new DataGridTextColumn();
              col.Binding = new Binding(colProperty);
              var spHeader = new StackPanel() { Orientation = Orientation.Horizontal };
              spHeader.Children.Add(new TextBlock(new Run(colProperty)));
              Button button1 = new Button();
              button1.Click += btnEdit_Click;
              button1.Content = "Edit";
              spHeader.Children.Add(button1);
              col.Header = spHeader;
              DgEmp.Columns.Add(col);
          }
  </code></pre>
  
  <p>And it looks like</p>
  
  <p><a href="https://i.stack.imgur.com/rXz4G.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rXz4G.png" alt="enter image description here"></a></p>