English 中文(简体)
Gridview: Export to csv issue
原标题:

I have the following scenario. I have two methods of populating a GridView with data. These are as follows:

protected void btSearch_Click(object sender, EventArgs e)
{

    lqPackWeights.WhereParameters.Clear();
    ControlParameter cp = new ControlParameter();
    cp.Type = TypeCode.String;


    if (radBuyer.Checked)
    {
        cp.ControlID = "ddlProd";
        cp.PropertyName = "SelectedValue";
        lbRadMiss.Text = "";
    }

    else if (radProd.Checked)
    {
        cp.ControlID = "tbxProdAC";
        cp.PropertyName = "Text";
        lbRadMiss.Text = "";
    }

    else
    {
        cp.ControlID = "lbRadMiss";
        cp.PropertyName = "Text";
        lbRadMiss.Text = "Please check appropriate radio button before you attempt a search";
    }

    cp.Name = "IDDesc";
    lqPackWeights.WhereParameters.Add(cp);
    GridView1.DataSourceID = "lqPackWeights";
    GridView1.DataBind();

}

And

protected void btnShowPaper_Click(object sender, EventArgs e)
{
    ORWeightsDataClassesDataContext dbPa = new ORWeightsDataClassesDataContext();
    int max = 0;
    if (int.TryParse(txtbxHowMany.Text, out max))
    {
        var queryPa = dbPa.tblOnlineReportingCOMPLETEWeights
                    .Where(x => x.MaterialLevel == "Primary" && x.MaterialText == "Paper" && x.MemberId == "FM00012")
                    .OrderByDescending(x => x.ProductPercentage).Take(max);

        GridView1.DataSource = queryPa;
        GridView1.DataBind();

These both work fine in populating the gridview with their approrpiate data set.

I also have the following code that exports the gridview content to csv:

protected void btnExportCSV_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv");
    Response.Charset = "";
    Response.ContentType = "application/text";

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

    StringBuilder sb = new StringBuilder();
    for (int k = 0; k < GridView1.Columns.Count; k++)
    {
        sb.Append(GridView1.Columns[k].HeaderText +  , );
    }

    sb.Append("
");
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        for (int k = 0; k < GridView1.Columns.Count; k++)
        {
            sb.Append(GridView1.Rows[i].Cells[k].Text +  , );
        }
        sb.Append("
");
    }
    Response.Output.Write(sb.ToString());
    Response.Flush();
    Response.End();

}

The export to csv works perfectly well in the first situation (i.e. the if/else if scenario) but when it comes to the second scenario, only the headings are extracted.

What s going on here then?! I have added in a few breaks and it looks like the export to csv code is being executed, however the actual GridView content is not placed onto the same csv file.

Any ideas?

最佳回答

Make sure you are rebinding your data BEFORE you do your export. I ran into this same issue a while back on a project, as we had VIEWSTATE turned off on the GridView, so on the postback we were trying to export the data, prior to the grid being populated with data.

问题回答

There is a javascript function you need to add. Check out this blog post which explains it:

http://www.inspiredbytechnology.com/index.php/2011/03/09/export-gridview-to-csv-in-a-sharepoint-2010-webpart/

Thanks

Jon

I actually resolved this issue by removing the GridView1.AllowPaging = false; GridView1.DataBind(); from the export script.

Not sure why it works...but it does!





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签