473,883 Members | 1,618 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 14703
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
6425
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
2041
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
4677
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 be inserted into the dataGridView? thanks for the help, Matthew
7
7347
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
5250
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
9637
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 of me, and I can't find it. Can someone point me in the right direction? -- Terry
4
4605
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 r = new MyDGVW (1, 2, 3); DataGridView.Rows.Add (r);
0
2075
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 clipboard, but it doesnt work fine. anybody tell me the solution to resolve this. my another question is,...
3
9705
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 record as much as needed to get it into a valid state before actually adding it to the DataTable.
0
9933
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11114
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10730
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10407
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9563
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7114
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5787
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.