Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can filter a date column in my datagridview but when i try to export it to excel it exports the whole datatable and not the filtered Datagridview.

These are part of what i have done so far

Filtering for the Date column which works

C#
private void BtnFilter_Click(object sender, EventArgs e)
       {
           bSource = new BindingSource();
           bSource.DataSource = eTable;
           dgv1.DataSource = bSource;
           bSource.Filter = string.Format("DateTime>= '{0:dd/MM/yyyy}' AND      DateTime <= '{1:dd/MM/yyyy}'", dtPickerStart.Value.Date, dtPickerStop.Value.Date);


Export event that exports whole datatable and not the filtered datagridview

C#
}
                    using (ExcelPackage pck = new ExcelPackage(file))
                   {
                       bSource = new BindingSource();
                       bSource.DataSource = dgv1;
                       dgv1.DataSource = eTable;

                       ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Sheet1");
                       ws.Cells["A1"].LoadFromDataTable(((System.Data.DataTable)dgv1.DataSource), true, OfficeOpenXml.Table.TableStyles.Light1);


I suspect that i am not using the bindingsource correctly.Any suggestions would be appreciated
Posted
Updated 23-May-15 8:36am
v2

1 solution

I found the solution on another site.I had to make use of the DataTable.DefaultView property to export the filtered datagridview.

This is the code to export the filterd datagridview
C#
using (ExcelPackage pck = new ExcelPackage(file))
                   {
                       BindingSource bs = (BindingSource)dgv1.DataSource;
                       System.Data.DataTable table = (System.Data.DataTable)bs.DataSource;
                       System.Data.DataTable filtered = table.DefaultView.ToTable();

                       ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Sheet1");
                       ws.Cells["A1"].LoadFromDataTable(((System.Data.DataTable)filtered), true, OfficeOpenXml.Table.TableStyles.Light1);
                       using (ExcelRange rng = ws.Cells[1, 1, 1, dgv1.Columns.Count])
                       {
                           rng.Style.Font.Bold = true;
                           rng.Style.Fill.PatternType = ExcelFillStyle.Solid;
                           rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(79, 129, 189));
                           rng.Style.Font.Color.SetColor(System.Drawing.Color.White);

                           ws.Column(3).Hidden = false;
                           ws.Cells["A1"].AutoFitColumns(25);
                           ws.Cells["B1"].Value = "Employee Number";
                           ws.Cells["B1"].AutoFitColumns(40);
                           ws.Cells["B1"].Value = "Employee Number";
                           ws.Cells["D1"].AutoFitColumns(60);
                           ws.Column(4).Style.Numberformat.Format = "dd/MM/yyyy";
                       }

                       pck.Save();
                   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900