是否可以正常导出没有复选框和链接按钮的gridview [英] Is it possible to export gridview without checkbox and linkbutton as normal

查看:45
本文介绍了是否可以正常导出没有复选框和链接按钮的gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

Iam将gridview数据导出到excel时,一切工作正常,在该gridview iam上,显示一列为复选框,另一列为linkbutton.
现在我不想在导出后显示复选框,并且linkbutton不会显示为链接.怎么可能,我尝试使用此代码,但没有得到.

任何帮助将不胜感激.

这是我使用的代码

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

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]);

            }

        }

    }

推荐答案

我看到您正在使用递归函数将LinkBut​​ton,DropDownlists和CheckBoxes删除为Literal,并显示文本而不是控件,但是呈现excel文件后,您不会撤消更改.

从外观上看,您在屏幕显示和excel上都使用相同的gridview.我建议您在不同的面板中有2个gridview,并根据请求类型使用gridview.

HTH!
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!


这篇关于是否可以正常导出没有复选框和链接按钮的gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆