title: Export selected rows AND columns datagridview
body: <p>I have a datagridview and I need to export only selected rows and columns to pdf using itextsharp. I am able to export whole datagridview and and also only selected rows. </p>
  
  <p>Problem 1. Rows are exported in opposite order, not how I checked them. (e.g I check rows ID 1, 5, 8 and in pdf it is 8, 5, 1)</p>
  
  <p>I do not want to change <strong><em>SelectionMode</em></strong>, because now I use <em>FullRowSelect</em> to load values to textboxes and to export selected rows. I found a possible answer here: <a href="https://stackoverflow.com/questions/23949660/get-selected-row-columns-count-without-setting-selection-mode">Get selected Row/Columns Count without Setting Selection Mode</a> but I am not able to use it.. </p>
  
  <p>Problem 2. I need user to select rows and also columns to export. </p>
  
  <p>here is my code to export my dgv:</p>
  
  <pre><code>private void exportDgvPDF(DataGridView dgvLoadAll, string filename)
     {
          BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.EMBEDDED);
  
          iTextSharp.text.Font text = new iTextSharp.text.Font(bf, 11, iTextSharp.text.Font.NORMAL);
  
          Document doc = new Document(PageSize.A2.Rotate(), 1, 1, 1, 1);
          PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(filename, FileMode.Create));
          doc.Open();
  
          // I need to remove 21 columns since I have lots invisible "useless" columns .. will work on that later
          PdfPTable pdftable = new PdfPTable(dgvLoadAll.ColumnCount - 21);
  
          for (int j = 0; j &lt; dgvLoadAll.Columns.Count - 21; j++)
          {
              PdfPCell cell = new PdfPCell(new Phrase(dgvLoadAll.Columns[j].HeaderText, text));
              cell.BackgroundColor = BaseColor.LIGHT_GRAY;
              pdftable.AddCell(cell);
          }
  
          pdftable.HeaderRows = 0;
          for (int i = 0; i &lt; dgvLoadAll.SelectedRows.Count; i++)
          //for (int i = 0; i &lt; dgvLoadAll.Rows.Count; i++)
          {
              for (int k = 0; k &lt; dgvLoadAll.Columns.Count - 21; k++)
              {
  
                  if (dgvLoadAll[k, i].Value != null)
                  {
                      pdftable.AddCell(new Phrase(dgvLoadAll.SelectedRows[i].Cells[k].Value.ToString(), text));
                      //pdftable.AddCell(new Phrase(dgvLoadAll[k, i].Value.ToString(), text));
  
                  }
              }
          }
  
          //float[] widths = new float[] { 15f, 50f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f };
  
         // pdftable.SetWidths(widths);
          doc.Add(pdftable);
          doc.Close(); 
      }
  </code></pre>