Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 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 April 15th, 2005, 02:22 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 449
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to r_ganesh76
Default Datagrid Export to Excel

Hi Experts,

I am held up with a problem. I have a datagrid in my aspx search page and I also have an "Export to Excel" button both of which will be displayed when a valid search is made. I have the following code written in my Export Button click event.

The grid contains LinkButtons inside one of the Template Columns and other bound columns.

when I export the grid to excel, I get the correct output the first time. However, if the grid data changes, I am not getting the changed result in the excel. The old data is retained all the time. If i reset the IIS and then do a fresh query, I get the correct result.

Can anyone please help me solve this issue.

Thanking in advance

Regards
Ganesh
__________________
Regards
Ganesh
http://ganeshprof.blogspot.com
Find your solution here...
 
Old April 15th, 2005, 02:26 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 449
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to r_ganesh76
Default

I thought the code which I used would be useful for solving this issue.

Code:
private void cmdExportToExcel_Click(object sender, System.EventArgs e)
{
    Response.Clear(); 
    Response.Buffer = true; 
    Response.ContentType = "application/vnd.ms-excel"; 
    Response.Charset = ""; 
    EnableViewState = false; 
    System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); 
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); 

    // Optional function call to convert the controls to string literals 
    ClearControls(MyGrid);     
    MyGrid.RenderControl(oHtmlTextWriter); 

    Response.Write(oStringWriter.ToString()); 
    Response.End(); 
}

private void ClearControls(Control Ctrl) 
{ 
    for (int i= Ctrl.Controls.Count - 1; i >= 0; i--) 
    { 
        ClearControls(Ctrl.Controls[i]); 
    } 
    string x = Ctrl.GetType().ToString();

    if (!(Ctrl is TableCell)) 
    { 
        if (Ctrl.GetType().GetProperty("SelectedItem") != null) 
        { 
            LiteralControl Lt = new LiteralControl(); 
            Ctrl.Parent.Controls.Add(Lt); 
            try 
            { 
                Lt.Text = (string)Ctrl.GetType().GetProperty("SelectedItem").GetValue(Ctrl, null); 
            } 
            catch {} 

            Ctrl.Parent.Controls.Remove(Ctrl); 
        } 
        else if (Ctrl is LinkButton)
        {
            LiteralControl Lt = new LiteralControl(); 

            Lt.Text = ((LiteralControl) Ctrl.Controls[0]).Text;
            Ctrl.Parent.Controls.Add(Lt); 
            Ctrl.Parent.Controls.Remove(Ctrl); 
        }
        else if (Ctrl.GetType().GetProperty("Text") != null) 
        { 
            LiteralControl Lt = new LiteralControl(); 
            Ctrl.Parent.Controls.Add(Lt); 
            Lt.Text = (string)Ctrl.GetType().GetProperty("Text").GetValue(Ctrl, null); 

            Ctrl.Parent.Controls.Remove(Ctrl); 
        }
    } 
    return; 
}
Regards
Ganesh
 
Old April 15th, 2005, 05:12 AM
Authorized User
 
Join Date: Jun 2004
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to gadhiav Send a message via Yahoo to gadhiav
Default

Hi Ganesh,
What happening is that you are not able to clear the objects value after the first runthrough of your code.
Just try to dispose off all the objects that you have created during your code just befreo completing execution.

Hope this works..
Cheers....
Vishal

Life is not the Triumph but the Struggle
 
Old April 19th, 2005, 04:15 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 449
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to r_ganesh76
Default

Thanx Vishal,

That was some problem with the browser settings. Everything is working fine when the same site is accessed from a different machienes.

Now I have another problem. I have implemeted paging in my datagrid. But I want all the rows to be exported to Excel. Is there any way to achieve this.

Thanking in advance

Regards
Ganesh





Similar Threads
Thread Thread Starter Forum Replies Last Post
Export DataGrid To Excel in vb.net jayakumar.cj ASP.NET 1.0 and 1.1 Basics 3 September 1st, 2010 03:09 AM
EXPORT DATAGRID TO EXCEL FORMATTING PROBLEM akshay144 VS.NET 2002/2003 2 October 31st, 2006 01:10 PM
export the contents of Datagrid to Excel file softsea VS.NET 2002/2003 0 October 4th, 2006 02:38 PM
export datagrid to excel with configured width sn00pyg4rfi3ld ASP.NET 1.x and 2.0 Application Design 0 October 25th, 2005 05:42 AM





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