C# - How to pass the data from dataGridView1 to another using a button click?

Viewed 1614

I have a dataGridView1, dataGridView2 and a ButtonAdd in one Form.

The user will:

1- select any one "Cell" or the "entire Row".

2-select multiple Rows

Then:

the data selected will be moved from dataGridView1 to dataGridView2 when the button clicked. that's all what i need to do.

My try:

I have tried this solution after many searches and it's almost done but there's a little problem I couldn't handle:

Full 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 > 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

Lets Debug:

just Note a one thing before starting:

  • the method for deleting rows (multi or single) is working fine in all cases.
  • the method for adding to the DGV2 is the problem, I had taken it from here ...it works fine when selecting a single row but not multiple.

1- if you have selected a one cell/row it will be added and removed successfully.

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.. why?!

Because here

var currentRow = ((DataRowView)dataGridView1.CurrentRow.DataBoundItem).Row;
            ((DataTable)dataGridView2.DataSource).ImportRow(currentRow);   

gets the current index of the existing rows in the DGV1 and iterate to the number of the selecting rows and add them to DGV2.

Screenshot:

image of how the process is done: image of how the process is done.

what i need to do: what i need to do

What should be done to solve this problem?

3 Answers

Instead of

ImportRow(currentRow)

use something like this:

table.Rows.Add(currentRow);

Let me know if that works!

Related