title: how to edit select row in datagrid in wpf
body: <p>i have a button column edit corresponding to each row in datagrid on click of that button i want to get correessponding row edit value how can we do that </p>
  
  <p>xaml code is :-</p>
  
  <pre><code>&lt;Window x:Class="Wpf_grid.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="326" Width="946"&gt;
  &lt;Grid&gt;
      &lt;DataGrid Name="MyDataGrid" Uid="MyDataGrid" AutoGenerateColumns="False" AlternationCount="2" SelectionMode="Single" Margin="0,31,0,0" &gt;
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=RegId}" IsReadOnly="True" Header="Registration Id" Width="sizeToHeader"/&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="sizeToHeader" /&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=FName}" Header="Father Name" Width="sizeToHeader" /&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=ContactNumber}" Header="Contact Number" Width="sizeToHeader" /&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=email}" Header="email" Width="sizeToHeader" /&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=password}" Header="password" Width="sizeToHeader" /&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=paddress}" Header="paddress" Width="sizeToHeader" /&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=pcity}" Header="pcity" Width="sizeToHeader" /&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=pstate}" Header="pstate" Width="sizeToHeader" /&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=ppinCode}" Header="ppinCode" Width="sizeToHeader" /&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=caddress}" Header="caddress" Width="sizeToHeader" /&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=ccity}" Header="ccity" Width="sizeToHeader" /&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=cstate}" Header="cstate" Width="sizeToHeader" /&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=cpinCode}" Header="cpinCode" Width="sizeToHeader" /&gt;
              &lt;DataGridTemplateColumn Header="Edit Row"&gt;
                  &lt;DataGridTemplateColumn.CellTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;Button Content="Edit" Click="btnEdit_Click"/&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/DataGridTemplateColumn.CellTemplate&gt;
              &lt;/DataGridTemplateColumn&gt;
              &lt;DataGridTemplateColumn Header="Delete Row"&gt;
                  &lt;DataGridTemplateColumn.CellTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;Button Content="Delete" Click="btnDelete_Click"/&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/DataGridTemplateColumn.CellTemplate&gt;
              &lt;/DataGridTemplateColumn&gt;
          &lt;/DataGrid.Columns&gt;
      &lt;/DataGrid&gt;
  
      &lt;Button Content="LoadCustomer" Name="LoadCustomer" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="0,5,0,0" Click="LoadCustomer_Click" /&gt;
  
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>and .cs code is :- </p>
  
  <pre><code>    private void btnEdit_Click(object sender, RoutedEventArgs e)
      {
          try
          {
              gridDataContext datacontext = new gridDataContext();
              registration registrationRow = MyDataGrid.SelectedValue as registration;
              int m = registrationRow.RegId;
              //registration Registration = (from p in datacontext.registrations where p.RegId == registrationRow.RegId select p).Single();
              registration Registration = datacontext.registrations.Where(A =&gt; A.RegId == m).Single();
              Registration.Name = registrationRow.Name;
              Registration.FName = registrationRow.FName;
              datacontext.SubmitChanges();
              MessageBox.Show("Row Updated Successfully");
              LoadCustomerDetail();
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
              return;
          }
      }
  </code></pre>
  
  <p>anyone tell me how can i edit on button click ??</p>