Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How do I move selected rows from one datagridview to another datagridview in a different form
 

How do I move selected rows from one datagridview to another datagridview in a different form

I am trying to write an address book where I can edit the contact by selecting the row on the datagridview, click the 'Edit' button and it will open my 'frmEdit' with the selected row in the datagridview in that form. I know how to update the database once it is in there I just cant seem to get the row to go in. I tried using the clipboard but it didn't work and to be honest it seems like a flawed way to copy rows.

Thanks

Padan  Thursday, March 27, 2008 11:23 AM

Hi, Padan

Youcaneither creating a propertyto pass the row orpassing the row as aparameter in the constructor of the frmEditform. For example,creating a property in the frmEdit form.

Code Snippet

partial class Form9 : Form

{

public Form9()

{

InitializeComponent();

}

private DataGridViewRow row;

public DataGridViewRow Row

{

get { return row; }

set { row = value; }

}

private void Form9_Load(object sender, EventArgs e)

{

DataGridViewRow r = row.Clone() as DataGridViewRow;

foreach (DataGridViewCell cell in row.Cells)

{

this.dataGridView1.Columns.Add(cell.OwningColumn.Name,

cell.OwningColumn.HeaderText);

r.Cells[cell.ColumnIndex].Value = cell.Value;

}

this.dataGridView1.Rows.Add(r);

}

}



Then before opening the frmForm, you should assign the current selected rowto the Row propertyof the frmEdit form, something like this

Code Snippet

void button1_Click(object sender, EventArgs e)

{

Form9 f = new Form9();

f.Row = this.dataGridView1.CurrentRow;

f.Show();

}



Zhi-Xin Ye  Tuesday, April 01, 2008 12:19 PM

Hi, Padan

Youcaneither creating a propertyto pass the row orpassing the row as aparameter in the constructor of the frmEditform. For example,creating a property in the frmEdit form.

Code Snippet

partial class Form9 : Form

{

public Form9()

{

InitializeComponent();

}

private DataGridViewRow row;

public DataGridViewRow Row

{

get { return row; }

set { row = value; }

}

private void Form9_Load(object sender, EventArgs e)

{

DataGridViewRow r = row.Clone() as DataGridViewRow;

foreach (DataGridViewCell cell in row.Cells)

{

this.dataGridView1.Columns.Add(cell.OwningColumn.Name,

cell.OwningColumn.HeaderText);

r.Cells[cell.ColumnIndex].Value = cell.Value;

}

this.dataGridView1.Rows.Add(r);

}

}



Then before opening the frmForm, you should assign the current selected rowto the Row propertyof the frmEdit form, something like this

Code Snippet

void button1_Click(object sender, EventArgs e)

{

Form9 f = new Form9();

f.Row = this.dataGridView1.CurrentRow;

f.Show();

}



Zhi-Xin Ye  Tuesday, April 01, 2008 12:19 PM
The above code is absolutely correct. The correct way to add a row from an existing datagridview is to clone the row and then add the text contents back into the row. The reason why cloning alone doesnt work is because the clone copies the structure of the row and not the data. If you do not clone the datagridviewrow then it will require that both datagridview's have the exact same columns visible.

foreach (DataGridViewRow dgvr in dataGridView2.SelectedRows)
                {
                    DataGridViewRow r = dgvr.Clone() as DataGridViewRow;
                    foreach (DataGridViewCell cell in dgvr.Cells)
                    {
                        r.Cells[cell.ColumnIndex].Value = cell.Value;
                    }
                    dataGridView2.Rows.Remove(dgvr);
                    dataGridView3.Rows.Add(r);
                }
markgolf123  Wednesday, October 07, 2009 9:11 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView, Business Objects, BindingList (Sorting Issues)
• Additional whitespaces during insert
• date format in datagrid ...?
• Bound TextBox text is not refreshing?
• DataGridViewCheckBoxColumn
• get a field value from a dbgrid
• Create a dataview using criteria from a child table.
• DataGridView Column format
• DISPLAYING AN INCREMENTING COUNT
• Managing data in DataGridView column