title: Exporting empty cells to csv comma-delimited file from datagridview
body: <p>First off, I am still fairly new to programming, so please forgive any lapses in knowledge or terminology I may offer.</p>
  
  <p>I am building a C# application that first imports data from an Excel workbook into a set of datagridviews (One dgv per worksheet). The user can select a custom range of cells from each datagrid, and then click a button to export the selected range to a CSV file. To this point, I have most of what I need working, but the problem I am having is that it is skipping over blank cells at the end, and I need those to be included in the final output of the csv. For example if cells A1 through A3 have a value, but A4 does not, and the user selects the range A1 through A4, the final csv needs to output as:</p>
  
  <pre><code>1,2,3,
  </code></pre>
  
  <p>However, the way I have it now, it only outputs <code>1,2,3</code>. If there are multiple blank cells at the end of the range, it does the same thing, and we need those accounted for.</p>
  
  <p>To note, if the blank cell occurs anywhere within the range, it works fine...it is only if the cells are empty at the beginning or end of the range that this seems to happen.</p>
  
  <p>If I reset any blank or null values to " ", it works as a workaround, but I would prefer the end-file to not have any blank-type values in it. </p>
  
  <p>I appreciate any help. Here is my code that handles this export functionality:</p>
  
  <pre><code>    private void exportClick(DataGridView dgv)
      {
          Int32 selectedCellCount = dgv.GetCellCount(DataGridViewElementStates.Selected);
          int CurrentRow = 0;
          int CurrentColumn = 0;
  
          Excel.Application xlApp;
          Excel.Workbook xlWorkBook;
          Excel.Worksheet xlWorkSheet;
          object misValue = System.Reflection.Missing.Value;
  
          xlApp = new Excel.Application();
          xlWorkBook = xlApp.Workbooks.Add(misValue);
          xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
  
          if (selectedCellCount &gt; 0)
          {
              for (int i = 0; i &lt; selectedCellCount; i++)
              {
                  // Get the Row and Column index values
                  CurrentRow = Convert.ToInt16(dgv.SelectedCells[i].RowIndex.ToString());
                  CurrentColumn = Convert.ToInt16(dgv.SelectedCells[i].ColumnIndex.ToString());
  
                  // Set cell to current cells' value
                  DataGridViewCell cell = dgv[CurrentColumn, CurrentRow];
  
                  // Check if the cell Value is empty, set to ??? so a comma is written
                  if (String.IsNullOrEmpty(cell.Value.ToString()))
                      cell.Value = " ";
  
                  // Write the value to the worksheet 
                  xlWorkSheet.Cells[CurrentRow + 1, CurrentColumn + 1] = cell.Value;
              }
  
              // Prompt to save the results as a csv file
              if (saveFileDialog1.ShowDialog() == DialogResult.OK)
              {
  
                  xlWorkBook.SaveAs(saveFileDialog1.FileName, Excel.XlFileFormat.xlCSV, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
              }
          }
  
          xlWorkBook.Close(true, misValue, misValue);
          xlApp.Quit();
  
          releaseObject(xlWorkSheet);
          releaseObject(xlWorkBook);
          releaseObject(xlApp);
      }
  </code></pre>