Link to home
Start Free TrialLog in
Avatar of Nelson3000
Nelson3000Flag for United States of America

asked on

Please Help... How do I freeze the heading row in a bound GridView?

Please Help... I need to freeze the heading row in a GridView.

I am utilizing the GridView in a routine that exports the contents of a DataTable to HTML, in Excel format.

Before I render the grid to HTML, I wanted to do some cell formatting , hoping that these customizations are then reflected in the exported XLS.

Here is my routine:

------

 Sub ExportSpreadsheet(dtExport As DataTable, strExportPathAndFilename As String)

   
        Dim gvReport As New GridView()

        gvReport.AllowPaging = False

        '-- Load the GridView
        gvReport.DataSource = dtExport
        gvReport.DataBind()

        gvReport.HeaderRow.BackColor = Color.LightGray


        For Each gvRow As GridViewRow In gvReport.Rows

            For Each gvCell As TableCell In gvRow.Cells

                If gvRow.RowIndex Mod 2 = 0 Then
                    gvCell.BackColor = Color.White
                Else
                    gvCell.BackColor = Color.AliceBlue
                End If

                gvCell.Wrap = False

                If IsDate(gvCell.Text) Then
                    gvCell.HorizontalAlign = HorizontalAlign.Center
                End If

            Next

        Next

     
        Dim swReport As New StringWriter()
        Dim hwReport As New HtmlTextWriter(swReport)

        gvReport.RenderControl(hwReport)


        '-- Write XLS-file to disk
        Dim streamReport As New StreamWriter(strExportPathAndFilename)
        streamReport.Write(swReport.ToString())
        streamReport.Close()

    End Sub

-----


Can I somehow integrate freezing the heading row into my routine where I loop through the cells?

Any help in accomplishing this would be greatly appreciated.

Until I get Excel.Office.Interop installed on the server, I need to utiize this HTML to Excel solution.
It is not perfect, but at least it gives my Users the immediate benefit of getting their data into a spreadsheet.

Many Thanks,

Nelson
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Hi Nelson3000,

Installing Office.Interop on the server is a bad idea for two reaons
1. Office is licensed as a server solution hence legal challenge HOWEVER, unless and until you are caught, it is a fair game.
2. Office is not designed to be a multi-thread safe solution so when it works it will work, when it breaks most probably it will be difficult to get support.

Now coming to your query, If your end users are going to open this file in Excel, I don't think currently we have a way out.

Regards,
Chinmay.
Avatar of Nelson3000

ASKER

Hi Chinmay,

Thanks for again providing me new insights.

My current client, without hesitation, would purchase the necessary licenses for Office.Interop.  And their current workload is under 50 users. Hence scalability should not be a problem. However, if you have a better "Export to Excel" solution, I would love to hear it.

As for my current spreadsheet, I have already rolled it out as is, and although not elegant, my Users are happy to have their data in a spreadsheet.

Programmatically freezing a row is so simple in Excel. I am very disappointed that to do so with a GridView, appears to not be possible.

Meanwhile, thanks again for your help.

Best Regards,

Nelson
Hi Nelson,

It is not about purchasing license at all. I am quoting this from a huge article published by Microsoft

Besides the technical problems, you must also consider licensing issues. Current licensing guidelines prevent Office applications from being used on a server to service client requests, unless those clients themselves have licensed copies of Office. Using server-side Automation to provide Office functionality to unlicensed workstations is not covered by the End User License Agreement (EULA).

Source: https://support.microsoft.com/en-us/help/257757/considerations-for-server-side-automation-of-office

Trust me, it is not a good idea. Now, alternatives are to purchase solutions OR use open source libraries which natively export to excel, for example,
1. NPOI - https://github.com/tonyqus/npoi/ 
2. EPPlus - I think someone already suggested it in another question
3. Infragistics, Telerik OR DevExpress Grids
4. Microsoft SQL Server Reporting Services

I am not sure if any of the above provides freeze header functionality.

Another, most simplest way is to export as CSV and ask your users to import the CSV in a template that you can provide with required format. There are lot of creative solutions around this - for example, a macro that can automatically pickup, parse and import the CSV once user has selected it. But those will be done on Excel side, not on the server side so any changes that you need to make will require you to publish and manage those changes on your clients as well. Many organizations have used this approach in past very successfully though.

Regards,
Chinmay.
I need to freeze the heading row in a GridView.
What do you mean by a "heading row"? Do you mean the first row, or do you mean the column headers?
Good Morning Chinmay,

Thanks very much for your detailed explanation about the downside of server side installation of Office.Interop.  
I also greatly appreciate your list of alternatives and the link to the Microsoft white paper.

As one would expect, the information that you provided has initiated an important discussion at my client's organization on how to best proceed.

I will keep you posted on the outcome and thank you again for steering me in the right direction.


Many thanks and best regards,

Nelson
Hi Nelson,

Thank you very much. Feel free to ask follow up question in case there are some push backs from the client side.
Also do note that the components I have listed are NOT everything, if you hunt around you may find even better alternatives ( As such I have not seen something beating NPOI and EPPLus in a long time).

Also couple of years back I wrote an important IP for my employer and instead of using Office.Interop and any other library, I used Open XML SDK : https://docs.microsoft.com/en-us/office/open-xml/open-xml-sdk
There is a learning curve with it - the idea behind this SDK is driven on the fact that each .***X file is nothing but a zip file with couple of XML files inside them. [Don't believe me? change the .xlsx to .zip and see it for yourself :)] - but it is a 100% replacement for Office Interop without any other consequences - except the increased development time(and related headache).

All the best for your quest.

Regards,
Chinmay.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.