Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

Can't convert GridView to Excel..

Hello-

This code should work but does not.  I populate a session variable with a GridView that has been binded to a populated datatable.  Then I simply call this session variable in the code attached in the screenshot and an error gets thrown at  gvs.RenderControl(htmlWrite) stating that the GridView needs to have a form tag of runat=server..?   That doesn't make any sense to me because the "Markup" of my webpage does have <asp:GridView ID="GridViewL" runat="server">

Can someone please help me..?

Thanks,
gridviewissue.gif
0
JoshinHtown
Asked:
JoshinHtown
  • 6
  • 5
  • 3
  • +1
1 Solution
 
kewlchap_85Commented:
Have a look at the below code, it works perfectly for me...

Public Sub To_Excel(ByVal dgv As DataGridView, ByVal pth As String)
        Dim xlApp As Object = CreateObject("Excel.Application")
        'create a new workbook
        Dim xlWB As Object = xlApp.Workbooks.Add
        Dim xlWS As Object = xlWB.Worksheets(1)
        'export the column headers
        For c As Integer = 0 To dgv.Columns.Count - 1
            xlWS.Cells(1, c + 1).Value = dgv.Columns(c).HeaderText
        Next
        'export the values
        For r As Integer = 0 To dgv.RowCount - 1
            For c As Integer = 0 To dgv.Columns.Count - 1
                xlWS.Cells(r + 2, c + 1).Value = dgv.Item(c, r).Value
            Next
        Next
        'save the workbook as the path specified
        xlWB.SaveAs(pth)
        xlWS = Nothing
        xlWB = Nothing
        xlApp.quit()
        xlApp = Nothing
    End Sub

Open in new window

0
 
JoshinHtownAuthor Commented:
Unfortunately I would rather not use any excel objects and references.  I simply want to grab the GridView and open it in Excel.  The code I posted has worked before but something isn't quite right.  Any suggestions from anyone?
0
 
aibusinesssolutionsCommented:
The error says that you do not have a <form runat="server"> around the gridview.  Did you check the code to make sure you have a form tag on the page?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
JoshinHtownAuthor Commented:
Hi-

In my "Markup" of that webpage the code reads this:

<asp:GridView ID="GridViewL" runat="server" Visible="False"></asp:GridView>

Do I need to say "form" somewhere?
0
 
Praveen VenuCommented:
try it like

<form runat="server">

</form>
0
 
JoshinHtownAuthor Commented:
Now I'm not getting any errors and Excel never pops up.  This is strange.. Shouldn't that code at least open a window asking the user if they want to save the excel file or open it??
0
 
Praveen VenuCommented:
it should work. Try clearing the cache
0
 
Praveen VenuCommented:
Also try Binding the Gridview once more as the first line of the event handler
0
 
JoshinHtownAuthor Commented:
Making some progress.. I'm now getting the attached error.  What do y'all think on this one?

Regards,
newerror.gif
0
 
aibusinesssolutionsCommented:
If you are using Ajax, you can't write an excel file to the stream like that, you'll have to redirect them to a seperate page to download the excel file.
0
 
JoshinHtownAuthor Commented:
Can you tell me how to do that?  I'm pretty new at programming and need some code help.

Regards,
0
 
aibusinesssolutionsCommented:
Actually, just add a postback trigger for the export button, not an asyncpostback.

<Triggers>
    <asp:PostBackTrigger ControlID="button1" />
</Triggers>

That will cause the export button to do a full postback, which will prevent the ajax error.
0
 
JoshinHtownAuthor Commented:
That seems like a good idea but I'm getting  Triggers is not supported and PostBackTrigger is not a known element.  Thoughts?

<Triggers><asp:PostBackTrigger ButtonID="Button5" runat="server" BackColor="Lime" Font-Size="Smaller" Height="19px" Style="z-index: 101; left: 160px; position: absolute; top: 64px" Text="Export Excel" Width="68px" /></Triggers>
0
 
aibusinesssolutionsCommented:
No the trigger should only contain what I have, that's it.

<Triggers>
    <asp:PostBackTrigger ControlID="Button5" />
</Triggers>

When you click button5, it will see that it is supposed to be a normal postback, and do a full postback.
0
 
aibusinesssolutionsCommented:
That should go in your update panel by the way.

<asp:UpdatePanel>
   <ContentTemplate>
   </ContentTemplate>
   <Triggers>
       <asp:PostBackTrigger ControlID="button5" />
   </Triggers>
</asp:UpdatePanel>
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now