Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi experts.

How to move the data from one grid view to another grid view.I want move the Selected Row from one grid view to another grid view when i click the Move button.

Now i just bind the data into grid view. And delete the selected Row.
try
                {
                    if (dataGridView2.Rows.Count != 0 && dataGridView2.SelectedRows[0].Index != dataGridView2.Rows.Count)
                    {
                        cn.Open();
                        cmd = new SqlCommand("DELETE FROM Phone_Number WHERE phone=" + dataGridView2.SelectedRows[0].Cells[1].Value.ToString() + "", cn);
                        cmd.ExecuteNonQuery();
                        dataGridView2.Rows.RemoveAt(dataGridView2.SelectedRows[0].Index);

                        MessageBox.Show("Deleted..!!");
                        cn.Close();
                    }

But i don't know how to move the selected rows to another grid view.
i try this code. But it is not working. It return the error Message.

private void button5_Click(object sender, EventArgs e)
       {
           dataGridView3.Rows.Add(dataGridView2.SelectedRows[0].Cells[1].Value.ToString());

           //dataGridView3.DataSource = dataGridView2.SelectedRows;

       }

The error message is: "No row can be added to a DataGridView control that does not have columns. Columns must be added first."

What i have to do?? Please Guide me.
Posted
Updated 8-Oct-13 3:31am
v5

 
Share this answer
 
First make sure that DataSource property is update for both DataGridView Controls using DATASET...

then on Button Click event.. transfer the Selected Row to DataSet where you want to move the record ...and ....then again update the DataSource Property of both DataGridView Controls...

Before update DataSource for second time... Don't forgot to DELETE the same Row from OLD table/Dataset of first DataGridView...


in simple steps...

1. Set Tables from Dataset as DataSource of both DataGridview controls
2. Make changes to Dataset as required while transferring any row(s)
3. Again, set update Tables from Dataset as DataSource of both DataGridview controls
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900