Copy and Paste rows in DataGridView

G

Guest

If any one knows how to implement functionality to copy(ctrl+c) an existing
row and paste(ctrl+v) the contents to create a new row in Datagrid view.

I know ctrl+c copies the content to the clipboard, i want "ctrl + v" to
insert the copied contents into a new row.

Any suggestions are appreciated.

Thanks
 
S

Sanjeevakumar Hiremath

Try this code.. you need to override WndProc in your own custom datagrid
control. Hope this helps.

protected override void WndProc(ref Message m)
{
IDataObject clipData = Clipboard.GetDataObject();
bool ctrlDown = false;
bool shiftDown = false;
bool handledMessage = false; // used to decide if call to base WndProc
needed.
switch ( m.Msg )
{
case 0x0100 :
{
if((int)m.WParam == 0x0011)
{
ctrlDown = true;
break;
}
if((int)m.WParam == 0x0010)
{
shiftDown = true;
break;
}
if((ctrlDown == true) && ((int)m.WParam == 0x0056))
{
handledMessage = PerformPaste();
break;
}
if((shiftDown == true) && ((int)m.WParam == 0x002D))
{
handledMessage = PerformPaste();
break;
}
}
break;
case 0x0101 :
{
if((int)m.WParam == 0x0011)
{
ctrlDown = false;
break;
}
if((int)m.WParam == 0x0010)
{
shiftDown = false;
break;
}
}
break;
case 0x0302 :
{
handledMessage = PerformPaste();
}
break;
}
if(handledMessage == true)
{
m.Result = new IntPtr( 0 );
}
else
{
base.WndProc(ref m);
}
}

private bool PerformPaste()
{
//custom past operation implementated by user
}
 

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