Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / September 2005

Tip: Looking for answers? Try searching our database.

Datagrid functionality

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
11Oppidan - 22 Sep 2005 15:59 GMT
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
"Jeffrey Tan[MSFT]" - 23 Sep 2005 10:00 GMT
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
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

"Jeffrey Tan[MSFT]" - 28 Sep 2005 07:09 GMT
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
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.