Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

Iam exporting gridview data to excel everything is working fine, on that gridview iam displaying one column is checkbox and another column is linkbutton.
Now i don't want to display the checkbox after export and linkbutton won't show as link. how is it possible and i tried with this code but I am not getting.

Any help will be appreciated.

This is the code what i used
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        PrepareGridViewForExport(GridView1);
        string attachment = "attachment; filename=KALYANMANTAP_BOOKED_LIST_BY_WEBMASTER.xls"; 

        Response.ClearContent();

        Response.AddHeader("content-disposition", attachment);

        Response.ContentType = "application/ms-excel";

        StringWriter stw = new StringWriter();

        HtmlTextWriter htextw = new HtmlTextWriter(stw);        
        GridView1.AllowPaging = false;
        bind();        
        GridView1.RenderControl(htextw);
        Response.Write(stw.ToString());
        Response.End();
    }

    private void PrepareGridViewForExport(Control gv)
    {

        LinkButton lb = new LinkButton();

        Literal l = new Literal();

        string name = String.Empty;

        for (int i = 0; i < gv.Controls.Count; i++)
        {

            if (gv.Controls[i].GetType() == typeof(LinkButton))
            {

                l.Text = (gv.Controls[i] as LinkButton).Text;

                gv.Controls.Remove(gv.Controls[i]);

                gv.Controls.AddAt(i, l);

            }

            else if (gv.Controls[i].GetType() == typeof(DropDownList))
            {

                l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;

                gv.Controls.Remove(gv.Controls[i]);

                gv.Controls.AddAt(i, l);

            }

            else if (gv.Controls[i].GetType() == typeof(CheckBox))
            {

                l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";

                gv.Controls.Remove(gv.Controls[i]);

                gv.Controls.AddAt(i, l);

            }

            if (gv.Controls[i].HasControls())
            {

                PrepareGridViewForExport(gv.Controls[i]);

            }

        }

    }
Posted
Updated 12-Jan-12 23:02pm
v2

1 solution

I see that you are using a recursive function to remove your LinkButtons, DropDownlists and CheckBoxes to Literals and display the text instead of the control but you are not reversing the change after rendering the excel file.

By the looks of it you are using the same gridview for both onscreen display and excel. I would suggest you to have 2 gridviews in differnt panels and use the gridviews based on the request types.

HTH!
 
Share this answer
 
Comments
swarup65 13-Jan-12 7:28am    
Hi dinesh,

Thank You for ur reply. I also think to do like that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900