vendredi 2 janvier 2015

Export selected rows AND columns datagridview [C#]


Vote count:

0




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.


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)


I do not want to change SelectionMode, because now I use FullRowSelect to load values to textboxes and to export selected rows. I found a possible answer here: Get selected Row/Columns Count without Setting Selection Mode but I am not able to use it..


Problem 2. I need user to select rows and also columns to export.


here is my code to export my dgv:


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 < 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 < dgvLoadAll.SelectedRows.Count; i++)
//for (int i = 0; i < dgvLoadAll.Rows.Count; i++)
{
for (int k = 0; k < 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(); }


asked 56 secs ago







Export selected rows AND columns datagridview [C#]

Aucun commentaire:

Enregistrer un commentaire