Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 28th, 2008, 01:17 PM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default [Resolved] GridView to Excel

I am exporting a GridView to Excel (code below) and it works fine except for that I am not able to export the current sorting on the GridView. It always takes the initial default used when I initially load the GridView.

In the "Excel_Button_Click" I use the saved "sortexpression" from the GridView1_Sorting event but I cannot get it to work.


Code: Export to Excel


Code:
Protected Sub Excel_Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Excel_Button.Click

        Dim GridView2 As New GridView
        GridView2 = GridView1
        GridView2.AllowPaging = False
        GridViewSortExpression = Session("SortExpression")       
        myAdsDataTable = Session("SalesTicketDataTable")
        GridView2.DataSource = SortDataTable(myAdsDataTable, False)
        GridView2.DataBind()

        Export("SalesTicketExport.xls", GridView2)

    End Sub


    Public Shared Sub Export(ByVal fileName As String, ByVal gv As GridView)
        HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fileName))
        HttpContext.Current.Response.ContentType = "application/ms-excel"
        Dim sw As IO.StringWriter = New IO.StringWriter
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        '  Create a form to contain the grid
        Dim table As Table = New Table
        table.GridLines = gv.GridLines
        '  add the header row to the table
        If (Not (gv.HeaderRow) Is Nothing) Then
            PrepareControlForExport(gv.HeaderRow)
            table.Rows.Add(gv.HeaderRow)
        End If
        '  add each of the data rows to the table
        For Each row As GridViewRow In gv.Rows
            PrepareControlForExport(row)
            table.Rows.Add(row)
        Next
        '  add the footer row to the table
        If (Not (gv.FooterRow) Is Nothing) Then
            PrepareControlForExport(gv.FooterRow)
            table.Rows.Add(gv.FooterRow)
        End If
        '  render the table into the htmlwriter
        table.RenderControl(htw)
        '  render the htmlwriter into the response
        HttpContext.Current.Response.Write(sw.ToString)
        HttpContext.Current.Response.End()
    End Sub

    ' Replace any of the contained controls with literals
    Private Shared Sub PrepareControlForExport(ByVal control As Control)
        Dim i As Integer = 0
        Do While (i < control.Controls.Count)
            Dim current As Control = control.Controls(i)
            If (TypeOf current Is LinkButton) Then
                control.Controls.Remove(current)
                control.Controls.AddAt(i, New LiteralControl(CType(current, LinkButton).Text))
            ElseIf (TypeOf current Is ImageButton) Then
                control.Controls.Remove(current)
                control.Controls.AddAt(i, New LiteralControl(CType(current, ImageButton).AlternateText))
            ElseIf (TypeOf current Is HyperLink) Then
                control.Controls.Remove(current)
                control.Controls.AddAt(i, New LiteralControl(CType(current, HyperLink).Text))
            ElseIf (TypeOf current Is DropDownList) Then
                control.Controls.Remove(current)
                control.Controls.AddAt(i, New LiteralControl(CType(current, DropDownList).SelectedItem.Text))
            ElseIf (TypeOf current Is CheckBox) Then
                control.Controls.Remove(current)
                control.Controls.AddAt(i, New LiteralControl(CType(current, CheckBox).Checked))
            ElseIf (TypeOf current Is Image) Then
                control.Controls.Remove(current)
            End If
            If current.HasControls Then
                PrepareControlForExport(current)
            End If
            i = (i + 1)
        Loop
    End Sub



Code :GridView Sort event



Code:
Protected Sub GridView1_Sorting1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting

        GridViewSortExpression = e.SortExpression
        Session("SortExpression") = e.SortExpression       
        myAdsDataTable = Session("SalesTicketDataTable")
        Dim pageIndex As Integer = GridView1.PageIndex
        GridView1.DataSource = SortDataTable(myAdsDataTable, False)
        GridView1.DataBind()
        GridView1.PageIndex = pageIndex

    End Sub
 
Old October 28th, 2008, 01:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hi there..

Probably you are rebinding the grid in the page load or somewhere else, and that code is executing before the exporting to excel.. check for that..

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old October 28th, 2008, 02:37 PM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Got the code working:

        
Code:
GridView1.AllowPaging = False
Code:
        GridViewSortExpression = Session("SortExpression")
        myAdsDataTable = Session("SalesTicketDataTable")
        Dim pageIndex As Integer = GridView1.PageIndex
        GridView1.DataSource = SortDataTable(myAdsDataTable, False)
        GridView1.DataBind()
        GridView1.PageIndex = pageIndex
        Export("SalesTicketExport1.xls", GridView1)
        GridView1.AllowPaging = True


I will be using this code for multiple application and would like to make it re-useable (similar to creating a class in windows app). How would I do that in ASP? Thank you.






Similar Threads
Thread Thread Starter Forum Replies Last Post
[Resolved] GridView - manually set SortExpression snufse ASP.NET 2.0 Basics 1 November 3rd, 2008 10:00 AM
[Resolved] GridView Sorting and Paging snufse ASP.NET 2.0 Basics 1 October 9th, 2008 07:45 AM
[Resolved] GridView TemplateField - get value snufse ASP.NET 2.0 Basics 25 July 29th, 2008 07:25 AM
[Resolved] IListSource problem in GridView snufse ASP.NET 2.0 Basics 5 February 12th, 2008 01:42 PM
Enable Editing on Gridview - RESOLVED Andrew.Berry ASP.NET 2.0 Basics 0 October 3rd, 2007 05:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.