title: C# wpf adding new row to another data table
body: <p>I have a product table which has 9 columns, I have another table which I only want to have 4 columns which is why I declared this</p>
  
  <pre><code>    private void SetCartColumns()
      {
          cartTable.Columns.Add("Product Code", typeof(Int32));
          cartTable.Columns.Add("Product Name", typeof(String));
          cartTable.Columns.Add("Product Quantity", typeof(Int32));
          cartTable.Columns.Add("Total Price", typeof(double));
      }
  </code></pre>
  
  <p>Now I want to add the selected index or item in my product table to the cart table, and here are the codes</p>
  
  <hr>
  
  <pre><code>    private void BtnAddToCart_Click(object sender, RoutedEventArgs e)
      {
          if (dtgProducts.SelectedItem == null)
          {
              MessageBox.Show("There is no selected rows!");// show a message here to inform
          }
          else
          {
              while (dtgProducts.SelectedItems.Count &gt; 0)
              {
                  int selectedIndex = dtgProducts.SelectedIndex;
                  var selectedRows = ((DataRow)searchTable.Rows[selectedIndex]);
                  int code = selectedRows.Field&lt;int&gt;(("Code"));
                  string name = selectedRows.Field&lt;string&gt;(("Name"));
                  int quan = selectedRows.Field&lt;int&gt;(("Quantity"));
                  float price = selectedRows.Field&lt;float&gt;(("Price"));
                  double totalPrice = Convert.ToDouble(price);
  
  
                  cartTable.Rows.Add(new Object[] { code, name, quan, totalPrice });
              }
              dtgCart.ItemsSource = cartTable.AsDataView();
          }
      }
  </code></pre>
  
  <hr>
  
  <p>but whenever I am clicking the add to cart button, nothing happens but instead my program hangs and I cant click on anything aside from stopping the debugging.. would someone show me how to add new item to a new table with selected columns only</p>