出口gridview的空行时CSV - 解决 [英] Gridview empty rows when exporting to CSV - SOLVED

查看:161
本文介绍了出口gridview的空行时CSV - 解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题已经促使通过<进一步讨论href=\"http://stackoverflow.com/questions/6674555/export-gridview-data-into-csv-file/6674589?noredirect=1#comment43079606_6674589\">this问题。

下面是我使用的用于创建CSV文件中的程序。
一切都很正常,但我只得到了列标题和任何行。

 保护无效CSVReport_Click(对象发件人,EventArgs的发送)
    {        Response.Clear();
        将Response.Buffer =真;
        Response.AddHeader(内容处置,附件;文件名= PeopleReportTest.csv);
        Response.Charset的=;
        Response.ContentType =TEXT / CSV;
        Response.AddHeader(杂注,公共);       GridView1.AllowSorting = FALSE;
       GridView1.AllowPaging = FALSE;
       GridView1.DataBind();        StringBuilder的SB =新的StringBuilder();
        对于(INT K = 1; K&LT; GridView1.Columns.Count; k ++)
        {
            //添加分隔符
            sb.Append(GridView1.Columns [k]的.HeaderText +',');
        }
        //追加新行
        sb.Append(\\ r \\ n);
        的for(int i = 0; I&LT; GridView1.Rows.Count;我++)
        {
            对于(INT K = 1; K&LT; GridView1.Columns.Count; k ++)
            {
                //添加分隔符
                sb.Append(GridView1.Rows [I] .Cells [K]。文本+',');
            }
            //追加新行
            sb.Append(\\ r \\ n);
        }
        Response.Output.Write(sb.ToString());
        Response.Flush();
        到Response.End();
    }

在检查,我可以看到 GridView1.Rows [I] .Cells [K]。文本总是返回空值。

因此​​,问题可能是与我的GridView的结构。我知道,如果我有code是行不通的模板列,但我只有一个,它的设置为不可见。

所有其他字段都是 DynamicFields 。莫非是这个问题?

下面的标记:

 &LT; ASP:GridView控件ID =GridView1=服务器
            的CssClass =表的表冷凝无国界
            HeaderStyle-Horizo​​ntalAlign =左
            AllowPaging =真AllowSorting =真
            的DataSourceID =GridDataSource
            的DataKeyNames =是PersonIDOnRowCommand =GridView1_RowCommand
            的AutoGenerateColumns =假
            OnDataBound =GridView1_DataBound每页=15&GT;
            &LT;柱体和GT;
                &LT; ASP:的TemplateField itemstyle宽度=0%可见=假&GT;
                    &LT;&ItemTemplate中GT;
                        &LT; ASPF:dynamichyperlink ID =detailshyperlink=服务器
                            文字=细节/&GT;
                    &LT; / ItemTemplate中&GT;                &LT; / ASP:的TemplateField&GT;                &LT; ASP:DynamicField的DataField =名称的HeaderText =名称/&GT;
                &LT; ASP:DynamicField的DataField =PersonCategory的HeaderText =类别/&GT;
                &LT; ASP:DynamicField的DataField =PositionDescription的HeaderText =位置/&GT;
                &LT; ASP:DynamicField的DataField =说明的HeaderText =主题/&GT;
                &LT; ASP:DynamicField的DataField =FellowsResponsibility的HeaderText =责任/&GT;
                &LT; ASP:DynamicField的DataField =PartnerSurname的HeaderText =合作伙伴/&GT;
            &LT; /专栏&GT;            &LT; PagerStyle的CssClass =页脚/&GT;
            &LT; PagerTemplate&GT;
                &LT; ASP:GridViewPager =服务器/&GT;
            &LT; / PagerTemplate&GT;
            &LT; EmptyDataTemplate&GT;
                目前在该表中没有的项目。
            &LT; / EmptyDataTemplate&GT;
&LT; / ASP:GridView的&GT;


解决方案

确定我发现了什么问题。

我无法找到其他相似的场景身边,所以我在回答别人的情况下我自己的问题就会发现它的帮助。

解决方案是简单地替代所有我的 DynamicFields 标准绑定列和BOOM所有的数据都在那里。

我不会使用 DynamicData 框架下一次,这是肯定的。

This question has been prompted by further discussion on this question.

The following is the routine I'm using for creating the CSV file. Everything works great, except that I only get the columns headers and no rows.

   protected void CSVReport_Click(object sender, EventArgs e)
    {

        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=PeopleReportTest.csv");
        Response.Charset = "";
        Response.ContentType = "text/csv";
        Response.AddHeader("Pragma", "public");

       GridView1.AllowSorting = false;
       GridView1.AllowPaging = false;
       GridView1.DataBind();

        StringBuilder sb = new StringBuilder();
        for (int k = 1; k < GridView1.Columns.Count; k++)
        {
            //add separator
            sb.Append(GridView1.Columns[k].HeaderText + ',');
        }
        //append new line
        sb.Append("\r\n");
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            for (int k = 1; k < GridView1.Columns.Count; k++)
            {
                //add separator
                sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
            }
            //append new line
            sb.Append("\r\n");
        }
        Response.Output.Write(sb.ToString());
        Response.Flush();
        Response.End();
    }

On inspection, I can see that GridView1.Rows[i].Cells[k].Text always returns an empty value.

So the issue might be with the structure of my gridview. I know that the code wouldn't work if I had TemplateFields, but I have only one and it's set to invisible.

All other fields are DynamicFields. Could that be the problem?

Here's the markup:

  <asp:GridView ID="GridView1" runat="server"
            CssClass="table table-condensed borderless"
            HeaderStyle-HorizontalAlign="Left"
            AllowPaging="True" AllowSorting="True"
            DataSourceID="GridDataSource"
            DataKeyNames="PersonID" OnRowCommand="GridView1_RowCommand"
            AutoGenerateColumns="false"
            OnDataBound="GridView1_DataBound" PageSize="15">
            <Columns>
                <asp:templatefield itemstyle-width="0%" visible="false">
                    <itemtemplate>
                        <aspf:dynamichyperlink id="detailshyperlink" runat="server"
                            text="details" />
                    </itemtemplate>

                </asp:templatefield>

                <asp:DynamicField DataField="Name" HeaderText="Name"/>
                <asp:DynamicField DataField="PersonCategory" HeaderText="Category" />
                <asp:DynamicField DataField="PositionDescription" HeaderText="Position" />
                <asp:DynamicField DataField="Description" HeaderText="Topic" />
                <asp:DynamicField DataField="FellowsResponsibility" HeaderText="Responsibility" />
                <asp:DynamicField DataField="PartnerSurname" HeaderText="Partner"/>
            </Columns>

            <PagerStyle CssClass="footer" />
            <PagerTemplate>
                <asp:GridViewPager runat="server" />
            </PagerTemplate>
            <EmptyDataTemplate>
                There are currently no items in this table.
            </EmptyDataTemplate>
</asp:GridView>

解决方案

OK I've found what the problem was.

I couldn't find other similar scenarios around, so I'm answering my own question in case someone will find it helpful.

The solution was simply to replace all my DynamicFields with standard BoundFields and BOOM all the data was there.

Next time I won't be using DynamicData framework, that's for sure.

这篇关于出口gridview的空行时CSV - 解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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