title: Adding New Row to DataGrid on button click
body: <p>I am new to WPF and got issues in adding new Row to Data-Grid on button click. I have tried few solutions but was not successful.</p>
  
  <p>Tried-
  If i enable "CanUserAddRows="True", This gives me buttons also which are present in upper rows. Also i dont want this approach as i want t add new row on button click.</p>
  
  <p><a href="https://i.stack.imgur.com/dohov.png" rel="nofollow noreferrer">I am attaching an image of how new row looks like if i use "CanUserAddRows="True" </a></p>
  
  <pre><code>&lt;StackPanel Name="Decrypt_Vault"&gt;
                              &lt;DataGrid  x:Name="gdDecryptVault" HorizontalAlignment="Left"  ColumnWidth="Auto" Margin="10,10,405,18"  AutoGenerateColumns="False" HorizontalGridLinesBrush="LightGray" VerticalGridLinesBrush="LightGray"&gt;
  
                                  &lt;DataGrid.Columns&gt;
                                      &lt;!--&lt;DataGridTextColumn Header="Host" Binding="{Binding Path=Host}" Width="*" IsReadOnly="True" /&gt;--&gt;
                                      &lt;DataGridTemplateColumn Header="Host"&gt;
                                          &lt;DataGridTemplateColumn.CellTemplate&gt;
                                              &lt;DataTemplate&gt;
                                                  &lt;TextBlock Text="{Binding Path=Host}"&gt;&lt;/TextBlock&gt;
                                              &lt;/DataTemplate&gt;
                                          &lt;/DataGridTemplateColumn.CellTemplate&gt;
                                          &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
                                              &lt;DataTemplate&gt;
                                                  &lt;TextBox Background="Aquamarine" Text="{Binding Path=Host, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/&gt;
                                              &lt;/DataTemplate&gt;
                                          &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
                                      &lt;/DataGridTemplateColumn&gt;
  
                                      &lt;!--&lt;DataGridTextColumn Header="Login" Binding="{Binding Path=Login}" Width="*" IsReadOnly="True" /&gt;--&gt;
                                      &lt;DataGridTemplateColumn Header="Login"&gt;
                                          &lt;DataGridTemplateColumn.CellTemplate&gt;
                                              &lt;DataTemplate&gt;
                                                  &lt;TextBlock Text="{Binding Path=Login}"&gt;&lt;/TextBlock&gt;
                                              &lt;/DataTemplate&gt;
                                          &lt;/DataGridTemplateColumn.CellTemplate&gt;
                                          &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
                                              &lt;DataTemplate&gt;
                                                  &lt;TextBox Background="Aquamarine" Text="{Binding Path=Login, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/&gt;
                                              &lt;/DataTemplate&gt;
                                          &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
                                      &lt;/DataGridTemplateColumn&gt;
  
                                      &lt;!--&lt;DataGridTextColumn Header="Password" Binding="{Binding Path=Password}" Width="*" IsReadOnly="True"/&gt;--&gt;
                                      &lt;DataGridTemplateColumn Header="Password" IsReadOnly="True"&gt;
                                          &lt;DataGridTemplateColumn.CellTemplate&gt;
                                              &lt;DataTemplate&gt;
                                                      &lt;TextBlock Name="LinePassword" Text="{Binding Path=Password}" Visibility="{Binding Path=IsPasswordVisible, Converter={StaticResource BoolToVis}}"&gt;&lt;/TextBlock&gt;
                                              &lt;/DataTemplate&gt;
                                          &lt;/DataGridTemplateColumn.CellTemplate&gt;
                                          &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
                                              &lt;DataTemplate&gt;
                                                  &lt;TextBox Background="Aquamarine" Text="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/&gt;
                                              &lt;/DataTemplate&gt;
                                          &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
                                      &lt;/DataGridTemplateColumn&gt;
  
                                      &lt;DataGridTemplateColumn Header="Password Actions" IsReadOnly="True"&gt;
                                          &lt;DataGridTemplateColumn.CellTemplate&gt;
                                              &lt;DataTemplate&gt;
                                                  &lt;DockPanel&gt;
                                                      &lt;Button  Click="bTogglePassword_Click" ToolTip="Toggle Password"
                                                          Name="bTogglePassword" Visibility="Visible"  Height="30" Width="30" &gt;
                                                          &lt;Image Source="/Images/button_login1.png" Stretch="Fill" Height="30" Width="30"/&gt;
                                                      &lt;/Button&gt;
                                                      &lt;Button Click="bCopyToClipBoard_Click" ToolTip="Copy Password to clipboard"
                                                          Name="bCopyToClipBoard" Visibility="Visible"  Height="30" Width="30" &gt;
                                                          &lt;Image Source="/Images/Copy_icon.png" Stretch="Fill" Height="30" Width="30"/&gt;
                                                      &lt;/Button&gt;
                                                  &lt;/DockPanel&gt;
                                                  &lt;!--&lt;TextBlock Text="{Binding Path=Password}"&gt;&lt;/TextBlock&gt;--&gt;
                                              &lt;/DataTemplate&gt;
                                          &lt;/DataGridTemplateColumn.CellTemplate&gt;
                                      &lt;/DataGridTemplateColumn&gt;
  
                                      &lt;!--&lt;DataGridTextColumn Header="Items" Binding="{Binding Path=Project.Name}" Width="*" IsReadOnly="True"/&gt;--&gt;
                                      &lt;DataGridTemplateColumn Header="Items" Width="100"&gt;
                                          &lt;DataGridTemplateColumn.CellTemplate&gt;
                                              &lt;DataTemplate&gt;
                                                  &lt;TextBlock Text="{Binding Path=Project.Name}"&gt;&lt;/TextBlock&gt;
                                              &lt;/DataTemplate&gt;
                                          &lt;/DataGridTemplateColumn.CellTemplate&gt;
                                          &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
                                              &lt;DataTemplate&gt;
                                                  &lt;TextBox Background="Aquamarine" Text="{Binding Path=Project.Name, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/&gt;
                                              &lt;/DataTemplate&gt;
                                          &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
                                      &lt;/DataGridTemplateColumn&gt;
  
                                      &lt;!--&lt;DataGridTextColumn Header="Created by" Binding="{Binding Path=CreatedBy}" Width="*" IsReadOnly="True"/&gt;--&gt;
                                      &lt;DataGridTemplateColumn Header="Created By" &gt;
                                          &lt;DataGridTemplateColumn.CellTemplate&gt;
                                              &lt;DataTemplate&gt;
                                                  &lt;TextBlock Text="{Binding Path=CreatedBy}"&gt;&lt;/TextBlock&gt;
                                              &lt;/DataTemplate&gt;
                                          &lt;/DataGridTemplateColumn.CellTemplate&gt;
                                          &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
                                              &lt;DataTemplate&gt;
                                                  &lt;TextBox Background="Aquamarine" Text="{Binding Path=CreatedBy, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/&gt;
                                              &lt;/DataTemplate&gt;
                                          &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
                                      &lt;/DataGridTemplateColumn&gt;
  
                                      &lt;!--&lt;DataGridTextColumn Header="Notes" Binding="{Binding Path=Notes}" Width="*" IsReadOnly="True"/&gt;--&gt;
                                      &lt;DataGridTemplateColumn Header="Notes" Width="100"&gt;
                                          &lt;DataGridTemplateColumn.CellTemplate&gt;
                                              &lt;DataTemplate&gt;
                                                  &lt;TextBlock Text="{Binding Path=Notes}"&gt;&lt;/TextBlock&gt;
                                              &lt;/DataTemplate&gt;
                                          &lt;/DataGridTemplateColumn.CellTemplate&gt;
                                          &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
                                              &lt;DataTemplate&gt;
                                                  &lt;TextBox Background="Aquamarine" Text="{Binding Path=Notes, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/&gt;
                                              &lt;/DataTemplate&gt;
                                          &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
                                      &lt;/DataGridTemplateColumn&gt;
  
                                      &lt;DataGridTemplateColumn Header="Actions" Width="100"&gt;
                                          &lt;DataGridTemplateColumn.CellTemplate&gt;
                                              &lt;DataTemplate&gt;
                                                  &lt;DockPanel&gt;
                                                      &lt;!--&lt;Button Content="Edit" Click="EditVaultLine"/&gt;--&gt;
                                                      &lt;Button  Click="EditVaultLine" ToolTip="Edit"
                                                          Name="Edit" Visibility="Visible"  Height="30" Width="30" &gt;
                                                          &lt;Image Source="/Images/edit.png" Stretch="Fill" Height="30" Width="30"/&gt;
                                                      &lt;/Button&gt;
                                                      &lt;Button  Click="DeleteVaultLine" ToolTip="Delete"
                                                          Name="Delete" Visibility="Visible"  Height="30" Width="30" &gt;
                                                          &lt;Image Source="/Images/delete.png" Stretch="Fill" Height="30" Width="30"/&gt;
                                                      &lt;/Button&gt;
                                                  &lt;/DockPanel&gt;
                                              &lt;/DataTemplate&gt;
                                          &lt;/DataGridTemplateColumn.CellTemplate&gt;
                                          &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
                                              &lt;DataTemplate&gt;
                                                  &lt;DockPanel &gt;
                                                      &lt;Button  Click="SaveEditedVaultLine" ToolTip="Save"
                                                          Name="Save" Visibility="Visible"  Height="30" Width="30" &gt;
                                                          &lt;Image Source="/Images/save2.png" Stretch="Fill" Height="30" Width="30"/&gt;
                                                      &lt;/Button&gt;
                                                      &lt;Button  Click="CancelEditVaultLine" ToolTip="Cancel"
                                                          Name="Cancel" Visibility="Visible"  Height="30" Width="30" &gt;
                                                          &lt;Image Source="/Images/erase.png" Stretch="Fill" Height="30" Width="30"/&gt;
                                                      &lt;/Button&gt;
                                                  &lt;/DockPanel&gt;
                                              &lt;/DataTemplate&gt;
                                          &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
                                      &lt;/DataGridTemplateColumn&gt;
  
                                  &lt;/DataGrid.Columns&gt;
                              &lt;/DataGrid&gt;
                          &lt;/StackPanel&gt;
  
  
  
  My .CS code to Bind data in Data-Grid
   var result = weekTaskView.getVaultRecordLines();
                  List&lt;VaultRecordLine&gt; list = new List&lt;VaultRecordLine&gt;();
                  foreach (var item in result.Entities)
                  {
                      VaultRecordLine vrl = new VaultRecordLine();
  
                      if (item.Attributes.Contains("createdby"))
                      {
                          vrl.CreatedBy = item.Attributes["createdby"].ToString();
                      }
                      if (item.Attributes.Contains("new_account"))
                      {
                          vrl.Host = item.Attributes["new_account"].ToString();
                      }
                      if (item.Attributes.Contains("new_login"))
                      {
                          vrl.Login = item.Attributes["new_login"].ToString();
                      }
                      if (item.Attributes.Contains("new_password"))
                      {
                          vrl.Password = item.Attributes["new_password"].ToString();
                      }
                      if (item.Attributes.Contains("new_vaultid"))
                      {
                          vrl.Id = new Guid(item.Attributes["new_vaultid"].ToString());
                      }
  
                      list.Add(vrl);
                  }
                  gdDecryptVault.ItemsSource = list;
  </code></pre>
  
  <p>Requirement- When i click 'Add New Row button', i need a new row in EDITABLE mode so that user can fill up data. Also i need 'Save' and 'Cancel 'buttons at the end of row to save that data or to 'Cancel' it respectively.</p>