title: C# DataGridView to Excel File errors
body: <p>I've coded this following a youtube tutorial, on the tutorial the DataSource is an AOD.NET Entity Data Model where I've used an Access Database instead. I've compiled the code but am getting various errors like</p>
  
  <p><code>The name 'productBindingSource' does not exist in the current context</code><br>
  or<br>
  <code>The name 'DB' does not exist in the current context</code><br>
  or<br>
  <code>The type or namespace name 'Product' could not be found</code></p>
  
  <p>I'm not sure if i've missed adding a reference or if these errors are due to the data source being different?</p>
  
  <p>Visual Studio automatically added the <code>//TODO: This line of code etc...</code> and I changed it to how it's showing in the tutorial.</p>
  
  <p>I'm hoping someone can help show me what i'm doing wrong?</p>
  
  <p>Tutorial: <a href="https://www.youtube.com/watch?v=-wGzK1vsqS8" rel="nofollow noreferrer">https://www.youtube.com/watch?v=-wGzK1vsqS8</a></p>
  
  <p><a href="https://i.stack.imgur.com/VtYql.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VtYql.png" alt="enter image description here"></a></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  
  namespace ExportWebsiteData
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
          private void label1_Click(object sender, EventArgs e)
          {
  
          }
  
          private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
          {
              List&lt;Product&gt; list = ((DataParameter)e.Argument).ProductList;
              string filename = ((DataParameter)e.Argument).FileName;
              Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
              Workbook wb = excel.Workbooks.Add(XlSheetType.xlWorksheet);
              Worksheet ws = (Worksheet)excel.ActiveSheet;
              excel.Visible = false;
              int index = 1;
              int process = list.Count;
              //Add Column
              ws.Cells[1, 1] = "Item Number";
              ws.cells[1, 2] = "Model";
              ws.cells[1, 3] = "Manufacturer";
              ws.cells[1, 4] = "Category";
              ws.cells[1, 5] = "Subcategory";
              //
              foreach(Product p in list)
              {
                  if (!backgroundWorker.CancellationPending)
                  {
                      backgroundWorker.ReportProgress(index++ * 100 / process);
                      ws.Cells[index, 1] = p.ItemNumber.ToString();
                      ws.Cells[index, 2] = p.Model.ToString();
                      ws.Cells[index, 3] = p.Manufacturer.ToString();
                      ws.Cells[index, 4] = p.Category.ToString();
                      ws.Cells[index, 5] = p.SubCategory.ToString();
                  }
              }
              //Save file
              ws.SaveAs(filename, XlFileFormat.xlWorkbookdefault, Type.Missing, Type.Missing, true, false, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
              excel.Quit();
          }
  
          struct DataParameter
          {
              public List&lt;Product&gt; ProductList;
              public string FileName { get; set; }
          }
  
          DataParameter _inputParameter;
  
          private void Form1_Load(object sender, EventArgs e)
          {
              using (this.inventoryTableAdapter.Fill(this._Wizard_Data_2016_10_17DataSet.Inventory); = new _Wizard_Data_2016_10_17DataSet())
              {
                  productBindingSource.DataSource = DB.Products.ToList();
              }
  
              // TODO: This line of code loads data into the '_Wizard_Data_2016_10_17DataSet.Inventory' table. You can move, or remove it, as needed.
              //this.inventoryTableAdapter.Fill(this._Wizard_Data_2016_10_17DataSet.Inventory);
  
          }
  
          private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
          {
              progressBar.Value = e.ProgressPercentage;
              lblStatus.Text = string.Format("Processing...{0}", e.ProgressPercentage);
              progressBar.Update();
          }
  
          private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
          {
              if(e.Error == null)
              {
                  Thread.Sleep(100);
                  lblStatus.Text = "Your data has been successfully exported.";
              }
          }
  
          private void btnExport_Click(object sender, EventArgs e)
          {
              if (backgroundWorker.IsBusy)
                  return;
              using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "Excel Workbook|*.xls" })
              {
                  if (sdf.ShowDialog() == DialogResult.OK)
                  {
                      _inputParameter.FileName = sfd.FileName;
                      _inputParameter.ProductList = productBindingSource.DataSource as List&lt;product&gt;;
                      progressBar.Minimum = 0;
                      progressBar.Value = 0;
                      backgroundWorker.RunWorkerAsync(_inputParameter);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>John's answer did fix my errors but the data-grid is now being populated by the cs code instead of a database. I've made a video explaining the problem in more detail if anyone can let me know what they think is the problem.</p>
  
  <p><a href="https://www.dropbox.com/s/1l5iw1j32a6oroj/C%23Excel.wmv?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/1l5iw1j32a6oroj/C%23Excel.wmv?dl=0</a></p>