开发者

Unable to Export the Dynamically added row to Gridview to Excel

开发者 https://www.devze.com 2023-03-25 19:25 出处:网络
I have tried Exporting GridView to Excel but observed that the Dynamically added last Row to Gridviewis not exported to excel.

I have tried Exporting GridView to Excel but observed that the Dynamically added last Row to Gridview is not exported to excel.

I have two datasets first one binds the data directly to Gridview.

After Which I add the last row from another DataSet.

In the page I'm able to see the result as Expected but when exported excel I'm not. Below is my code:

DataSet dsgrid = SqlHelper.ExecuteDataset(DBConnectionString.ConnectionString, CommandType.StoredProcedure, "usp_Training_GetCirclescoreCardReport  ", sqlparam);

    if (TrainingUtil.isDataSetValid(dsgrid))
    {

        RSGScoreCard_Grid.DataSource = dsgrid;
        RSGScoreCard_Grid.DataBind();
        AddOverallRow(dsgrid);
    }
    else RSGScoreCard_Grid.DataBind();

Adding Overall开发者_运维问答 row at bottom:

 #region Add OverallRow
private void AddOverallRow(DataSet dsgrid)
{
    using (GridViewRow gr = new GridViewRow(RSGScoreCard_Grid.Rows.Count + 1, 0, DataControlRowType.DataRow, DataControlRowState.Normal))
    {
        for (int i = 0; i < 6; i++)//6 is the column count for overall row
        {
            using (TableCell tc = new TableCell())
            {
                gr.Cells.Add(tc);
                if (i == 0)
                {
                    gr.Cells[i].ColumnSpan = 4;
                    gr.Cells[i].Text = "Overall";
                    gr.Cells[i].Attributes.Add("class", "fcol");
                    gr.Cells[i].Attributes.Add("style", "font-weight:bold;padding-left:20%");
                }
                else gr.Cells[i].Attributes.Add("style", "font-weight:bold");

            }
        }


        if (dsgrid.Tables[1] != null)//creating a dynamic row to gridview
            if (dsgrid.Tables[1].Rows.Count > 0)
            {
                gr.Cells[1].Text = dsgrid.Tables[1].Rows[0][5].ToString();
                gr.Cells[1].Width = Unit.Percentage(8);
                gr.Cells[2].Text = dsgrid.Tables[1].Rows[0][6].ToString();
                gr.Cells[2].Width = Unit.Percentage(8);
                gr.Cells[3].Text = dsgrid.Tables[1].Rows[0][7].ToString();
                gr.Cells[3].Width = Unit.Percentage(8);
                gr.Cells[4].Text = dsgrid.Tables[1].Rows[0][8].ToString();
                gr.Cells[4].Width = Unit.Percentage(8);
                gr.Cells[5].Text = dsgrid.Tables[1].Rows[0][9].ToString();
                gr.Cells[5].Width = Unit.Percentage(8);
            }
        gr.Attributes.Add("class", "row2");

        RSGScoreCard_Grid.Controls[0].Controls.AddAt(RSGScoreCard_Grid.Rows.Count + 1, gr);


    }
}
#endregion

and Last my code to Export the GrieView:

 protected void btnExport_Click(object sender, EventArgs e)
{
    TrainingUtil.Export(ddlOptions.SelectedItem.Text.ToString().Replace(" ", string.Empty) + "_" + ddlVerticals.SelectedItem.Text.ToString().Replace(" ", string.Empty) + "_" + ddlLernerGroups.SelectedItem.Text.ToString().Replace(" ", string.Empty), RSGScoreCard_Grid, "For the Month/Year: " + ddlFromMonths.SelectedItem.Text.ToString()+"/"+ddlYears.SelectedItem.Text.ToString(), RSGScoreCard_Grid.HeaderRow.Cells.Count);
}
public override void VerifyRenderingInServerForm(Control control)
{
    /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
       server control at run time. */
}

the Export Method in TrainingUtil class

  #region Export

public static void Export(string filename, GridView grid, string Heading, int ColumnsCount)
{

    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", filename + ".xls"));
    HttpContext.Current.Response.ContentType = "application/ms-excel";
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter htw = new HtmlTextWriter(sw))
        {
            grid.HeaderStyle.BackColor = System.Drawing.Color.Cyan;
            //Cells color settings
            GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
            TableCell cell = new TableCell();
            cell.Text = String.Format("{0}", Heading);
            cell.ColumnSpan = ColumnsCount;
            cell.Attributes.Add("align", "center");
            cell.Attributes.Add("class", "yellow");
            row.Cells.Add(cell);
            grid.Controls[0].Controls.AddAt(0, row);
            foreach (GridViewRow gridRow in grid.Rows)
            {
                foreach (TableCell tcGridCells in gridRow.Cells)
                {
                    tcGridCells.Attributes.Add("class", "sborder");
                }
            }

            grid.RenderControl(htw);
            //Add the style sheet class here
            HttpContext.Current.Response.Write(@"<style> .sborder { color : Black;border : 1px Solid Black; } .yellow {background-color:yellow;color:black;} </style> ");
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
        }
    }

}
#endregion

Can any help me out.Why I'm not able to export the last row. Thanks in advance


I think in every post back your not binding the dynamically added rows. Try to find the control which cause the postback and bind the data once again.

Code to find the postback control ex:-

 public static Control GetPostBackControl(Page page)
{
    Control control = null;

    string ctrlname = page.Request.Params.Get("__EVENTTARGET");
    if (ctrlname != null && ctrlname != string.Empty)
    {
        control = page.FindControl(ctrlname);
    }
    else
    {
        foreach (string ctl in page.Request.Form)
        {
            Control c = page.FindControl(ctl);
            if (c is System.Web.UI.WebControls.Button)
            {
                control = c;
                break;
            }
        }
    }
    return control;
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消