views:

295

answers:

3

Morning all - another day, another question from Rich!

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("\r\n");
    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("\r\n");
    }
    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?

A: 

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.

mattruma
Hello Mattruma - thanks for looking into this. Apologies for my ignorance but how should I go about rebinding the data i.e. where should I put my databind?I feel like such a tool asking these questions as I know they are most likely, ridiculously easy.
MrDean
Not a problem! We usually have a method in our code behind called BindData() that binds the data to the grid, does any filtering as well. In our export method we always called BindData() first, then do the exporting.
mattruma
A: 

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!

MrDean
A: 

why the coding on the string builder was underline with green colour when try to accommodate with my button event handler..they ask me something "assembly refference" that im not understand..im a new in c# actually..please help..

komeng