0
votes

I'm pulling company schedule data from a web API and formatting the data so it can be displayed neatly on a local site and also available to export onto an excel template for distribution across the company.

I've added a button on the bottom of the site to display the previous weeks schedule and another to export the data to excel. When "Previous Week" is clicked it populates the same Gridview1 table with last weeks data. But When I click the export button it exports the data from the current week into Excel.

Here's some of my code, let me know what else I need to post for help.

The datatable gets set on page load into a gridview:

protected void Page_Load(object sender, EventArgs e)
        {

// ..... bunch of code above to call web API for date range and format table....

            DataView view = table.AsDataView();
            view.Sort = "Sort ASC";

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

            if (GridView1.Columns.Count > 0)
                GridView1.Columns[8].Visible = false;
            else
            {
                GridView1.HeaderRow.Cells[8].Visible = false;
                foreach (GridViewRow gvr in GridView1.Rows)
                {
                    gvr.Cells[8].Visible = false;
                }
            }

and the gridview gets pulled into a datatable and sent to an excel spreadsheet

protected void ExportExcel(object sender, EventArgs e)
        {
            DataTable dt = new DataTable("Resident Schedule");
            foreach (TableCell cell in GridView1.HeaderRow.Cells)
            {
                dt.Columns.Add(cell.Text);
            }
            foreach (GridViewRow row in GridView1.Rows)
            {
                dt.Rows.Add();
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    dt.Rows[dt.Rows.Count - 1][i] = row.Cells[i].Text;
                }
            }
            dt.Columns.Remove("Rotation");
            dt.Columns.Remove("Sort");

            using (XLWorkbook wb = new XLWorkbook(@"C:\template.xlsx"))
            {
                IXLWorksheet ws = wb.Worksheet("Resident Schedule");
                var rangeWithStrings = ws.Cell(5, 2).InsertTable(dt.AsEnumerable(), false);

// ..... bunch of code below

now I have a button click method that gets called to load the previous week into GridView:

protected void PreviousWeek(object sender, EventArgs e)
        {

// ..... bunch of code above to pull and sort last weeks schedule....

            DataView view = table.AsDataView();
            view.Sort = "Sort ASC";

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

            if (GridView1.Columns.Count > 0)
                GridView1.Columns[8].Visible = false;
            else
            {
                GridView1.HeaderRow.Cells[8].Visible = false;
                foreach (GridViewRow gvr in GridView1.Rows)
                {
                    gvr.Cells[8].Visible = false;
                }
            }

So, my question is, if the button click is repopulating GridView1 data, why is the ExportExcel method is going back to the Page Load data?

1

1 Answers

0
votes

Click on button, send postback event and seems you missed to check whether request is postback or not. so whenever you click on button then always gridview populated with current week data. Check postback request and then poulate gridview on load event.

if (!IsPostBack)
{
     // GridView Populate code
}