Adding a copy of a row in a DataGridView

M

michael sorens

I tried to do a simple operation on a Windows Form in VS2005 inside a
key_down handler:

if (e.Control && e.Shift && e.KeyCode == Keys.V)
{
int selectedRowIndex = dataGridView.SelectedCells[0].RowIndex;
dataGridView.Rows.AddCopy(selectedRowIndex);
}

So when the user presses Ctrl-Shft-V, a copy of the first row of a user's
selection is added to the bottom of the DataGridView, just before the new
row.

In reality, I get a new row but it is blank. What am I missing?
 
L

Linda Liu [MSFT]

Hi Michael,

I performed a test based on your code snippet and did see the same thing as
you did. When I press Ctrl-Shift-V, a new row is added to the DataGridView
control and the new row is blank.

The DataGridViewRowCollection.AddCopy method adds a new row based on the
row at the specified index. But it doesn't means the new row will copy the
values of the row positioned at the specified index. Instead, it means the
InheritedStyle property of the new row has the same values as the
InheiritedStyle of the row positioned at the specified index. That's to
say, the new row has the same DataGridViewCellStyle as the specified row
does.

If you change the DefaultCellStyle property of a row and then add a new row
based on this row by calling the DataGridViewRowCollection.AddCopy method,
you will see the new row has the same DataGridViewCellStyle as this row.
The following is a sample code to change the DefaultCellStyle property of a
row.

this.dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Azure;

You could add the above line of code in your application to have a test.

To copy the values of the specified row to the new row, you should do it
manually. The following is a sample.

void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.Shift && e.KeyCode == Keys.V)
{
int selectedRowIndex =
this.dataGridView1.SelectedCells[0].RowIndex;
int i = this.dataGridView1.Rows.AddCopy(selectedRowIndex);
// copy the values of the row positioned at
selectedRowIndex to the new row
this.dataGridView1.Rows.Cells[0].Value =
this.dataGridView1.Rows[selectedRowIndex].Cells[0].Value;
this.dataGridView1.Rows.Cells[1].Value =
this.dataGridView1.Rows[selectedRowIndex].Cells[1].Value;
.....
}
}


Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

michael sorens

Re-reading the API for AddCopy, you are correct: it does not explicitly
say it copies the data though it sure implies that from the name! Thanks
for pointing out the distinction...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top