Hi 11Oppidan,
Thanks for your post. I will answer your questions one by one:
#1, No, Winform datagrid is a highly encapsulated control, it does not
expose the interface for us to do this.
#2, Yes, when pressing Ctrl+C, Winform datagrid copies its content into
clipboard in certain DataFormats.Text. If we examine the clipboard data, we
will see that cells value in the clipboard are separated by '\t' character,
and each lines are separated by '\r\n', so we can trap Ctrl+C keys and add
the data to clipboard ourselves. Sample code like this:
public class MyDataGrid : System.Windows.Forms.DataGrid
{
- private const int WM_KEYDOWN=0x0100;
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if(msg.Msg==WM_KEYDOWN)
{
if((keyData&Keys.Control)==Keys.Control)
{
if((keyData&Keys.C)==Keys.C)
{
string str=string.Empty;
for(int
index=0;index<this.TableStyles[0].GridColumnStyles.Count;index++)
{
str+=this.TableStyles[0].GridColumnStyles[index].HeaderText+'\t';
}
str=str.Substring(0, str.Length-1);
str+=Environment.NewLine;
CurrencyManager
cm=(CurrencyManager)this.BindingContext[this.DataSource, this.DataMember];
for(int i=0;i<cm.Count;i++)
{
for(int j=0;j<this.TableStyles[0].GridColumnStyles.Count;j++)
{
str+=this[i, j].ToString()+'\t';
}
str=str.Substring(0, str.Length-1);
str+=Environment.NewLine;
}
Clipboard.SetDataObject(str);
return true;
}
}
}
return base.ProcessCmdKey (ref msg, keyData);
}
}
Note: this code assumes that you explicitly set the column style for the
datagrid.
This code works well on my side. Hope this helps.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.