473,839 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can i copy and paste the data


I have one VB.NET application,in that application i have
one datagrid control.The datagrid control contains
somedata.I want to copy the data in a particular cell and
paste it into my C#.NET application or notepad where i
want to paste it to be.How can i do it?If anyone knows
source code or any website URL's please let me know.

Nov 20 '05 #1
3 2885
Hi Uma,

There are so many possibilities with drag and drop and the clipboard, here 2

Hi x



I hope this helps a little bit?

Nov 20 '05 #2
You can use the dragDrop functionality of your controls , check this free
source example
using System;
using System.Drawing;
using System.Windows. Forms;

namespace Snip_DragNDrop
public class Form1 : System.Windows. Forms.Form
private System.Windows. Forms.ListBox ListDragSource;
private System.Windows. Forms.ListBox ListDragTarget;
private System.Windows. Forms.CheckBox UseCustomCursor sCheck;
private System.Windows. Forms.Label DropLocationLab el;

private int indexOfItemUnde rMouseToDrag;
private int indexOfItemUnde rMouseToDrop;

private Rectangle dragBoxFromMous eDown;
private Point screenOffset;

private Cursor MyNoDropCursor;
private Cursor MyNormalCursor;

/// The main entry point for the application.
static void Main()
Application.Run (new Form1());

public Form1()
this.ListDragSo urce = new System.Windows. Forms.ListBox() ;
this.ListDragTa rget = new System.Windows. Forms.ListBox() ;
this.UseCustomC ursorsCheck = new System.Windows. Forms.CheckBox( );
this.DropLocati onLabel = new System.Windows. Forms.Label();

this.SuspendLay out();

// ListDragSource
this.ListDragSo urce.AllowDrop = true;
this.ListDragSo urce.Items.AddR ange(new object[] {"one", "two", "three",
"five", "six", "seven", "eight",
"nine", "ten"});
this.ListDragSo urce.Location = new System.Drawing. Point(10, 17);
this.ListDragSo urce.Size = new System.Drawing. Size(120, 225);
this.ListDragSo urce.MouseDown += new
System.Windows. Forms.MouseEven tHandler(this.L istDragSource_M ouseDown);
this.ListDragSo urce.QueryConti nueDrag += new
System.Windows. Forms.QueryCont inueDragEventHa ndler(this.List DragSource_Quer y
this.ListDragSo urce.MouseUp += new
System.Windows. Forms.MouseEven tHandler(this.L istDragSource_M ouseUp);
this.ListDragSo urce.MouseMove += new
System.Windows. Forms.MouseEven tHandler(this.L istDragSource_M ouseMove);
this.ListDragSo urce.GiveFeedba ck += new
System.Windows. Forms.GiveFeedb ackEventHandler (this.ListDragS ource_GiveFeedb a
this.ListDragSo urce.DragDrop += new
System.Windows. Forms.DragEvent Handler(this.Li stDragTarget_Dr agDrop);
// ListDragTarget
this.ListDragTa rget.AllowDrop = true;
this.ListDragTa rget.Location = new System.Drawing. Point(154, 17);
this.ListDragTa rget.Size = new System.Drawing. Size(120, 225);
//this.ListDragTa rget.DragOver += new
System.Windows. Forms.DragEvent Handler(this.Li stDragTarget_Dr agOver);
this.ListDragTa rget.DragDrop += new
System.Windows. Forms.DragEvent Handler(this.Li stDragTarget_Dr agDrop);
this.ListDragTa rget.DragEnter += new
System.Windows. Forms.DragEvent Handler(this.Li stDragTarget_Dr agEnter);
//this.ListDragTa rget.DragLeave += new
System.EventHan dler(this.ListD ragTarget_DragL eave);

// UseCustomCursor sCheck
this.UseCustomC ursorsCheck.Loc ation = new System.Drawing. Point(10, 243);
this.UseCustomC ursorsCheck.Siz e = new System.Drawing. Size(137, 24);
this.UseCustomC ursorsCheck.Tex t = "Use Custom Cursors";

// DropLocationLab el
this.DropLocati onLabel.Locatio n = new System.Drawing. Point(154, 245);
this.DropLocati onLabel.Size = new System.Drawing. Size(137, 24);
this.DropLocati onLabel.Text = "None";

// Form1
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 270);
this.Controls.A ddRange(new System.Windows. Forms.Control[]
{this.ListDragS ource,
this.ListDragTa rget, this.UseCustomC ursorsCheck,
this.DropLocati onLabel});
this.Text = "Drag and Drop Example";

this.ResumeLayo ut(false);


private void ListDragSource_ MouseDown(objec t sender,
System.Windows. Forms.MouseEven tArgs e)
// Get the index of the item the mouse is below.
indexOfItemUnde rMouseToDrag = ListDragSource. IndexFromPoint( e.X, e.Y);

if (indexOfItemUnd erMouseToDrag != ListBox.NoMatch es)

// Remember the point where the mouse down occurred. The DragSize
// the size that the mouse can move before a drag event should be
Size dragSize = SystemInformati on.DragSize;

// Create a rectangle using the DragSize, with the mouse position being
// at the center of the rectangle.
dragBoxFromMous eDown = new Rectangle(new Point(e.X - (dragSize.Width
e.Y - (dragSize.Heigh t /2)), dragSize);
// Reset the rectangle if the mouse is not over an item in the ListBox.
dragBoxFromMous eDown = Rectangle.Empty ;


private void ListDragSource_ MouseUp(object sender,
System.Windows. Forms.MouseEven tArgs e)
// Reset the drag rectangle when the mouse button is raised.
dragBoxFromMous eDown = Rectangle.Empty ;

private void ListDragSource_ MouseMove(objec t sender,
System.Windows. Forms.MouseEven tArgs e)

if ((e.Button & MouseButtons.Le ft) == MouseButtons.Le ft)

// If the mouse moves outside the rectangle, start the drag.
if (dragBoxFromMou seDown != Rectangle.Empty &&
!dragBoxFromMou seDown.Contains (e.X, e.Y))

// Create custom cursors for the drag-and-drop operation.
MyNormalCursor = new Cursor("3dwarro .cur");
MyNoDropCursor = new Cursor("3dwno.c ur");

// An error occurred while attempting to load the cursors, so use
// standard cursors.
UseCustomCursor sCheck.Checked = false;

// The screenOffset is used to account for any desktop bands
// that may be at the top or left side of the screen when
// determining when to cancel the drag drop operation.
screenOffset = SystemInformati on.WorkingArea. Location;

// Proceed with the drag and drop, passing in the list item.

DragDropEffects dropEffect =
ListDragSource. DoDragDrop(List DragSource.Item s[indexOfItemUnde rMouseToDrag],
DragDropEffects .All | DragDropEffects .Link);

// If the drag operation was a move then remove the item.
if (dropEffect == DragDropEffects .Move)
ListDragSource. Items.RemoveAt( indexOfItemUnde rMouseToDrag);

// Selects the previous item in the list as long as the list has an
if (indexOfItemUnd erMouseToDrag > 0)
ListDragSource. SelectedIndex = indexOfItemUnde rMouseToDrag -1;

else if (ListDragSource .Items.Count > 0)
// Selects the first item.
ListDragSource. SelectedIndex =0;

// Dispose of the cursors since they are no longer needed.
if (MyNormalCursor != null)
MyNormalCursor. Dispose();

if (MyNoDropCursor != null)
MyNoDropCursor. Dispose();
private void ListDragSource_ GiveFeedback(ob ject sender,
System.Windows. Forms.GiveFeedb ackEventArgs e)
// Use custom cursors if the check box is checked.
if (UseCustomCurso rsCheck.Checked )

// Sets the custom cursor based upon the effect.
e.UseDefaultCur sors = false;
if ((e.Effect & DragDropEffects .Move) == DragDropEffects .Move)
Cursor.Current = MyNormalCursor;
Cursor.Current = MyNoDropCursor;

private void ListDragTarget_ DragOver(object sender,
System.Windows. Forms.DragEvent Args e)

// Determine whether string data exists in the drop data. If not, then
// the drop effect reflects that the drop cannot occur.
if (!e.Data.GetDat aPresent(typeof (System.String) ))

e.Effect = DragDropEffects .None;
DropLocationLab el.Text = "None - no string data.";

// Set the effect based upon the KeyState.
if ((e.KeyState & (8+32)) == (8+32) &&
(e.AllowedEffec t & DragDropEffects .Link) == DragDropEffects .Link)
// KeyState 8 + 32 = CTL + ALT

// Link drag and drop effect.
e.Effect = DragDropEffects .Link;

else if ((e.KeyState & 32) == 32 &&
(e.AllowedEffec t & DragDropEffects .Link) == DragDropEffects .Link)

// ALT KeyState for link.
e.Effect = DragDropEffects .Link;

else if ((e.KeyState & 4) == 4 &&
(e.AllowedEffec t & DragDropEffects .Move) == DragDropEffects .Move)

// SHIFT KeyState for move.
e.Effect = DragDropEffects .Move;

else if ((e.KeyState & 8) == 8 &&
(e.AllowedEffec t & DragDropEffects .Copy) == DragDropEffects .Copy)

// CTL KeyState for copy.
e.Effect = DragDropEffects .Copy;

else if ((e.AllowedEffe ct & DragDropEffects .Move) ==
DragDropEffects .Move)

// By default, the drop action should be move, if allowed.
e.Effect = DragDropEffects .Move;

e.Effect = DragDropEffects .None;

// Get the index of the item the mouse is below.

// The mouse locations are relative to the screen, so they must be
// converted to client coordinates.

indexOfItemUnde rMouseToDrop =
ListDragTarget. IndexFromPoint( ListDragTarget. PointToClient(n ew
Point(e.X, e.Y)));

// Updates the label text.
if (indexOfItemUnd erMouseToDrop != ListBox.NoMatch es)

DropLocationLab el.Text = "Drops before item #" +
(indexOfItemUnd erMouseToDrop + 1);
DropLocationLab el.Text = "Drops at the end.";

private void ListDragTarget_ DragDrop(object sender,
System.Windows. Forms.DragEvent Args e)
// Ensure that the list item index is contained in the data.
if (e.Data.GetData Present(typeof( System.String)) )

Object item = (object)e.Data. GetData(typeof( System.String)) ;

// Perform drag and drop, depending upon the effect.
if (e.Effect == DragDropEffects .Copy ||
e.Effect == DragDropEffects .Move)

// Insert the item.
if (indexOfItemUnd erMouseToDrop != ListBox.NoMatch es)
ListDragTarget. Items.Insert(in dexOfItemUnderM ouseToDrop, item);
ListDragTarget. Items.Add(item) ;

// Reset the label text.
DropLocationLab el.Text = "None";
private void ListDragSource_ QueryContinueDr ag(object sender,
System.Windows. Forms.QueryCont inueDragEventAr gs e)
// Cancel the drag if the mouse moves off the form.
ListBox lb = sender as ListBox;

if (lb != null)

Form f = lb.FindForm();

// Cancel the drag if the mouse moves off the form. The screenOffset
// takes into account any desktop bands that may be at the top or left
// side of the screen.
if (((Control.Mous ePosition.X - screenOffset.X) < f.DesktopBounds .Left)
((Control.Mouse Position.X - screenOffset.X) > f.DesktopBounds .Right) ||
((Control.Mouse Position.Y - screenOffset.Y) < f.DesktopBounds .Top) ||
((Control.Mouse Position.Y - screenOffset.Y) > f.DesktopBounds .Bottom))

e.Action = DragAction.Canc el;
private void ListDragTarget_ DragEnter(objec t sender,
System.Windows. Forms.DragEvent Args e)
// Reset the label text.
DropLocationLab el.Text = "None";
private void ListDragTarget_ DragLeave(objec t sender, System.EventArg s e)
// Reset the label text.
DropLocationLab el.Text = "None";

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 20 '05 #3
Hi Mohamoss,

That code will not compile in VB.Net


Nov 20 '05 #4

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

Similar topics

by: cv | last post by:
Hi all, I have to copy two set of data from 2 files(notepad/excel) say, products and their corresponding prices to list/textarea/table. I should be able to retrieve the product and corresponding price information from the control(list/textarea/table). Which is the best way? Quite urgent.. pls let me know. TIA, cv
by: DraguVaso | last post by:
Hi, I'm looking for a way to Copy and Paste Files to the clipboard. I found a lot of articles to copy pieces of text and bitmaps etc, but nog whole files. Whay I need is like you have in windows explorer: when you do a right-click on a file and choose Copy, and than paste it somewhere in my application and vice versa.
by: Keith | last post by:
I'm trying to come up with a way to create a contextmenu that will do all the "standard" functions (cut, copy, paste, undo, etc). There seems to be a lot of information out there - but nothing seems to work for me. A few people refer to txt.copy() txt.paste(), etc. I'm not sure if those are old functions, but they do not work for me. I've tried textbox1.selectall() - that works in selecting
by: Darrell Wesley | last post by:
Is there any way to copy an entire row from the Data Grid and then Paste it into a new row, modify some data and then save it back to the database? This grid is bound to a dataadapter with a commandbuilder monitoring the activity. Is there some property setting that needs to be set or what?
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard is proving to be more difficult. These pictureboxes are bound to an AccessDB. If the user wants to add an image, they select an image using an OpenFileDialog: Dim result As DialogResult = Pic_Sel.ShowDialog() If (result = DialogResult.OK) Then
by: NetronProject | last post by:
My copy/paste operation results in a MemoryStream full of '\0' characters on the Clipboard. Maybe my approach to copy/paste is wrong when handling generic types. All entries in the CollectionBase are, of course, ISerializable and the binary serialization to file fully works. <Thank you so much for you help>
by: Kaur | last post by:
Hi, I have been successful copying a vba code from one of your posts on how to copy and paste a record by declaring the desired fields that needs to be copied in form's declaration and creating two button "copy" and "paste". Works like magic. My problem is how can I copy multiple records and paste them at the same time. My data entry form has main form that has a Questionlist box of Questions. The second list box on the same form displays...
by: Tom | last post by:
I need to be able to copy and paste data from an Excel spreadsheet to a grid or something to a web form and then save it to the database. example: if my spreadsheet has 20 rows of data I need to be able to copy all 20 rows of data and paste them to a grid or something to on my web form and save it. I can't copy the data line by line or cell by cell, I need to be able to do Select ALL copy then paste. is there anyway or anything to allow...
by: Steve | last post by:
I'm trying to code cut, copy, and paste in vb 2005 so that when the user clicks on a toolbar button, the cut/copy/paste will work with whatever textbox the cursor is current located in (I have about 20 textboxes on the form). Also to ensure that the button can't get used if the cursor isn't in a textbox field. And to ensure the contents of the clipboard are "text" contents that have been cut/copied from one of the textboxes on the form. ...
by: damiencarr | last post by:
i have a spreadsheet/worksheet called "NTH_Alliance" containing rows of related data grouped together under different headings, that are named as different weeks (e.g. Week 5 (26/01/2009 - 1/02/2009), or Week 6 (2/02/2009 - 8/02/2009)). im writing a macro where the user enters a specific week they wish to see data for (e.g. Week 5 (26/01/2009 - 1/02/2009)), and once the week is found (which can be on any row in the spreadsheet - i.e. not fixed...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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...
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...
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 most users, this new feature is actually very convenient. If you want to control the update process,...
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...
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.