Groups | Blog | Home
all groups > dotnet windows forms controls > september 2005 >

dotnet windows forms controls : Datagrid functionality



11Oppidan
9/22/2005 12:00:00 AM
Please could someone show me how to do the following in VB .NET:



1. Freeze a column in a datagrid similar to the functionally in Excel where
you can 'Freeze Pane' - so when you scroll right you can see the first
column.

2. When a user selects all and copies the cells in a datagrid in order to
paste them into Excel, also copy the datagrid header columns.



Many thanks

v-jetan NO[at]SPAM online.microsoft.com (
9/23/2005 9:00:12 AM
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.
v-jetan NO[at]SPAM online.microsoft.com (
9/28/2005 6:09:25 AM
Hi 11Oppidan,

Does my reply make sense to you? Is your problem resolved? Please feel free
to tell me, thanks

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.
AddThis Social Bookmark Button