473,563 Members | 2,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding a copy of a row in a DataGridView

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 selectedRowInde x = dataGridView.Se lectedCells[0].RowIndex;
dataGridView.Ro ws.AddCopy(sele ctedRowIndex);
}

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?
Nov 3 '06 #1
2 14676
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 DataGridViewRow Collection.AddC opy 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 DataGridViewCel lStyle as the specified row
does.

If you change the DefaultCellStyl e property of a row and then add a new row
based on this row by calling the DataGridViewRow Collection.AddC opy method,
you will see the new row has the same DataGridViewCel lStyle as this row.
The following is a sample code to change the DefaultCellStyl e property of a
row.

this.dataGridVi ew1.Rows[0].DefaultCellSty le.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_K eyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.Shift && e.KeyCode == Keys.V)
{
int selectedRowInde x =
this.dataGridVi ew1.SelectedCel ls[0].RowIndex;
int i = this.dataGridVi ew1.Rows.AddCop y(selectedRowIn dex);
// copy the values of the row positioned at
selectedRowInde x to the new row
this.dataGridVi ew1.Rows[i].Cells[0].Value =
this.dataGridVi ew1.Rows[selectedRowInde x].Cells[0].Value;
this.dataGridVi ew1.Rows[i].Cells[1].Value =
this.dataGridVi ew1.Rows[selectedRowInde x].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/subscripti...ult.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/subscripti...t/default.aspx.
=============== =============== =============== =====

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

Nov 6 '06 #2
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...
Nov 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6399
by: dbuchanan | last post by:
Hello, Is this a bug? Is there some kind of work around? I want to add default values for a few columns in my datagridview I found the "DefaultValuesNeeded" event for the datagridview I gave it a try using the example given in
2
2017
by: bob | last post by:
After adding a new row (programmatically) to a table/view that is being displayed in the DataGridView control, the new row appears in the DataGridView grid. Now, how do I select that new row? If the DataGridView is sorted by one of the columns/fields, the new row could show up anywhere (based on the sort criteria). Thanks.
0
4606
by: MKBender | last post by:
dear all, I have a dataGridView that has cut,copy, and paste functionality for the cells of the datagrid but now I would like to add the functionality for the user to copy, cut and paste an entire row and rows. Can any one give me some code on how to use the clipboard for entire rows? I don't know how to format the data in a way that it can...
7
7332
by: BillE | last post by:
What is the best way to add data using a DataGridView in a multi-tier application (data entry is handled in a data access layer using stored procedures)? Thanks Bill
1
5211
by: The Tarm | last post by:
Hi everyone, I found resources on the web to do a copy on a datagridview control but i havent found any on pasting back to the control. I m trying to figure out how to do a shallow copy on a datagrid row. if (c is DataGridView) { DataGridView d = (DataGridView)c;
4
9621
by: =?Utf-8?B?VGVycnk=?= | last post by:
I have a DataGridView bound to a collection of Custom Objects. I have enabled adding new rows, but since my object does not have a public constructor (it uses a factory method), I get an error when I attempt to enter the new row. My guess is that I need to override some method so that I can create the object for the grid. But for the life...
4
4593
by: Dom | last post by:
I'm just playing around, to confirm my mental picture of what is happening in CSharp. Looks like I got the wrong mental picture, because I thought this would work. I created a class, MyDGVR, which extends the DataGridViewRow, and just added a little functionality by adding a property called NewProperty. Then I did the following: MyDGVR...
0
2026
nirmalsingh
by: nirmalsingh | last post by:
hi all, i am using datatable and dataset to assign data into datagridview. now i want to print data of datagridview contents. i used a method to copy datagridview content in a clipboard and to paste it in a print document. it is working fine but data is unaligned, so i think that if i apply borderstyle to datagridview and then copy to...
3
9674
by: jehugaleahsa | last post by:
Hello: I am binding a DataGridView with a BindingList<T>, where T is a custom business object that implements INotifyPropertyChanged. When you bind a DataGridView to a DataTable, it has this cool little feature - it will not call DataTable.Rows.Add until after you leave the DataGridView row. This is cool because it lets your user edit the...
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.