title: Insert, Update, and Delete rows directly in DataGrid and save changes in database
body: <p>I have a <code>ListView</code> with a list of tables from my SQL Server database. I also have a <code>Selected</code> event for my list view items, which populate my <code>DataGrid</code> with the table in the database. </p>
  
  <p>I would like to have the user be able to <code>INSERT, UPDATE, &amp; DELETE</code> rows in the selected table and save those changes in the database. </p>
  
  <p>Here is my code that populates the <code>DataGrid</code> when the <code>ListView</code> item is selected. I removed some to not make this too long: </p>
  
  <pre><code>    public partial class TableManagement : UserControl
  {
      public TableManagement()
      {
          InitializeComponent();
      }
  
  
        private void CustomerContact_Selected(object sender, RoutedEventArgs e)
      {
          try
          {
              string connectionString = ("Data Source=WINDOWS-B1AT5HC\\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");
              DataTable dt = new DataTable();
              SqlConnection connection = new SqlConnection(connectionString);
              SqlDataAdapter cmd = new SqlDataAdapter("SELECT [TXT_CUST_CNT], [Inactive] FROM [hb_CustCntct] ", connection);
              cmd.Fill(dt);
              dt.AcceptChanges();
              dt_TableManagement.DataContext = dt;
  
              dt_TableManagement.SelectedIndex = 0;
  
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
  </code></pre>
  
  <p>Here is my <code>XMAL</code> code for the <code>listVeiw</code> and <code>DataGrid</code></p>
  
  <pre><code>            &lt;!--Grid 1--&gt;
          &lt;Grid x:Name="Grid1" Grid.Row="1" Grid.Column="0" Margin="15"&gt;
              &lt;ListView&gt;
                  &lt;ListViewItem x:Name="CustomerContact" Content="Customer Contact" Selected="CustomerContact_Selected"/&gt;
                  &lt;ListViewItem x:Name="CsrNames" Content="Customer Service Representative Names" Selected="CsrNames_Selected"/&gt;
                  &lt;ListViewItem x:Name="DisputeClassification" Content="Dispute Classification" Selected="DisputeClassification_Selected"/&gt;
                  &lt;ListViewItem x:Name="DisputeStatus" Content="Dispute Status" Selected="DisputeStatus_Selected"/&gt;
                  &lt;ListViewItem x:Name="FinancialAdjustment" Content="Financial Adjustment" Selected="FinancialAdjustment_Selected"/&gt;
                  &lt;ListViewItem x:Name="InitialCallAnalysis" Content="Initial Call Analysis" Selected="InitialCallAnalysis_Selected"/&gt;
                  &lt;ListViewItem x:Name="InitialCallReason" Content="Initial Call Reason" Selected="InitialCallReason_Selected"/&gt;
                  &lt;ListViewItem x:Name="MeterIssue" Content="Meter Issue" Selected="MeterIssue_Selected"/&gt;
                  &lt;ListViewItem x:Name="OpenInError" Content="Open In Error" Selected="OpenInError_Selected"/&gt;
                  &lt;ListViewItem x:Name="PrimaryCause" Content="Primary Cause" Selected="PrimaryCause_Selected"/&gt;
                  &lt;ListViewItem x:Name="RequestedWork" Content="Requested Work" Selected="RequestedWork_Selected"/&gt;
                  &lt;ListViewItem x:Name="RevenueClass" Content="Revenue Class" Selected="RevenueClass_Selected"/&gt;
                  &lt;ListViewItem x:Name="ServiceOrderDetails" Content="Service Order Details" Selected="ServiceOrderDetails_Selected"/&gt;
                  &lt;ListViewItem x:Name="ServiceOrderType" Content="Service Order Type" Selected="ServiceOrderType_Selected"/&gt;
                  &lt;ListViewItem x:Name="ServiceOrderAdjustment" Content="Service Order Adjusted" Selected="ServiceOrderAdjustment_Selected"/&gt;
                  &lt;ListViewItem x:Name="UnderlyingCause" Content="Underlying Cause" Selected="UnderlyingCause_Selected"/&gt;
                  &lt;ListViewItem x:Name="UtilityReportType" Content="Utility Report Type" Selected="UtilityReportType_Selected"/&gt;
                  &lt;ListViewItem x:Name="WFMIssuedBy" Content="WFM Issued By" Selected="WFMIssuedBy_Selected"/&gt;
              &lt;/ListView&gt; 
          &lt;/Grid&gt;
  
          &lt;!--Grid 2--&gt;
          &lt;Grid x:Name="Grid2" Grid.Row="1" Grid.Column="2" Margin="15"&gt;
              &lt;DataGrid x:Name="dt_TableManagement"
                materialDesign:ListBoxAssist.IsToggle="True"
                Background="White"
                EnableColumnVirtualization="True"
                EnableRowVirtualization="True"
                ItemsSource="{Binding}"&gt;
              &lt;/DataGrid&gt;
          &lt;/Grid&gt;
  </code></pre>