title: C# - How to pass the data from dataGridView1 to another using a button click?
body: <p>I have a <code>dataGridView1</code>, <code>dataGridView2</code> and a <code>ButtonAdd</code> in one Form.</p>
  
  <p>The user will:</p>
  
  <p>1- select any one "Cell" or the "entire Row".</p>
  
  <p>2-select multiple Rows</p>
  
  <p>Then:</p>
  
  <p>the data selected will be moved from dataGridView1 to dataGridView2 when the button clicked. that's all what i need to do.</p>
  
  <p><strong>My try:</strong></p>
  
  <p>I have tried this solution after many searches and it's almost done but there's a little problem I couldn't handle:</p>
  
  <p><strong>Full Code:</strong></p>
  
  <pre><code>private const string strconneciton = @"YourConnectionString";
      SqlConnection con = new SqlConnection(strconneciton);
      SqlCommand cmd = new SqlCommand();
      DataTable dataTable;
  
  private void loadDataIntoGridView1()
      {
          try
          {
              con.Open();
              cmd.CommandText = "SELECT id, accNum, accName FROM Employees";
              cmd.Connection = con;
  
              SqlDataAdapter adapter = new SqlDataAdapter();
              adapter.SelectCommand = cmd;
  
              dataTable = new DataTable();
              adapter.Fill(dataTable);
  
              BindingSource bSource = new BindingSource();
              bSource.DataSource = dataTable;
              dataGridView1.DataSource = bSource;
  
              //i don't know if this line is useful...
              dataGridView2.DataSource = dataTable.Clone();
  
              adapter.Update(dataTable);
              con.Close();
  
          }
          catch (Exception ed)
          {
              con.Close();
              MessageBox.Show(ed.Message);
          }
      }//end loadDataIntoGridView1
  
  
  private void buttonSend_Click(object sender, EventArgs e)
  {       
      if (dataGridView1.SelectedCells.Count &gt; 0)
      {
          foreach (DataGridViewCell oneCell in dataGridView1.SelectedCells)
          {
              if (oneCell.Selected)
              {
                  //this should add the rows that is selected from dataGridView1 and,
                  //pass it to dataGridView2
                  var currentRow = ((DataRowView)dataGridView1.CurrentRow.DataBoundItem).Row;
                   ((DataTable)dataGridView2.DataSource).ImportRow(currentRow);
  
                  //this will remove the rows you have selected from dataGridView1
                  dataGridView1.Rows.RemoveAt(oneCell.RowIndex);
              }//end if
          }//end foreach
      }//end if
  }//end button click
  </code></pre>
  
  <p><strong>Lets Debug:</strong></p>
  
  <p>just <strong>Note</strong> a one thing before starting:</p>
  
  <ul>
  <li>the method for deleting rows (multi or single) is working fine in all cases.</li>
  <li>the method for adding to the DGV2 is the problem, I had taken it from <a href="https://stackoverflow.com/a/25614471/7797911">here</a> ...it works fine when selecting a single row but not multiple.</li>
  </ul>
  
  <p>1- if you have selected a one cell/row it will be added and removed successfully.</p>
  
  <p>2- if you have selected a multiple rows lets say the first and the second it will add the second and the third, then definitely will deleted them, but only one is added..
  <strong>why?!</strong> </p>
  
  <p>Because here </p>
  
  <pre><code>var currentRow = ((DataRowView)dataGridView1.CurrentRow.DataBoundItem).Row;
              ((DataTable)dataGridView2.DataSource).ImportRow(currentRow);   
  </code></pre>
  
  <p>gets the current index of the <strong>existing rows</strong> in the DGV1 and iterate to the number of the selecting rows and add them to DGV2.</p>
  
  <p><strong>Screenshot:</strong></p>
  
  <p>image of how the process is done:
  <a href="https://i.stack.imgur.com/sIGPT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sIGPT.png" alt="image of how the process is done"></a>.</p>
  
  <p>what i need to do:
  <a href="https://i.stack.imgur.com/D11cI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D11cI.png" alt="what i need to do"></a></p>
  
  <p>What should be done to solve this problem?</p>