tags:

views:

283

answers:

2

I am creating an Excel spreadsheet in C#. I like to have the header (first) row pinned in place when the user scrolls the rows. How can I do this in C# (or VB.NET)?

+3  A: 

I know that this not a fully detailed answer, but it should help you in the right direction. When I previously did a lot of Perl and later Ruby automation of Excel and wanted to know how to achieve this and that I usually recorded a macro and inspected its code to see how VBA interacted with the objects. I also did so for your task and this is what I got:

Sub Makro1()
'
' Makro1 Makro
'

'
    ActiveWindow.SplitRow = 1.1
    With ActiveWindow
        .SplitColumn = 0
        .SplitRow = 1
    End With
    ActiveWindow.FreezePanes = True
End Sub

I will leave it to somebody else to translate into C#, but it should be a walk in the park.

Christian Madsen
That looks easier than I expected it to be.
RedFilter
That's a simple, but neat trick. Nice answer :)
Scott Anderson
This worked. Thanks.
Tony_Henrich
A: 

This should do it in C#.

Private freezePain ()
{
    ...initialize objects

    ExcelObject.ActiveWindow.FreezePanes = false;
    WorksheetObject.get_Range(yourRange).Select();
    ExcelObject.ActiveWindow.FreezePanes = true;
}
Irwin M. Fletcher
For some reason more rows and fewer columns are frozen even though I have my range correct.
Tony_Henrich