2014-07-10 2 views
-1

iTextSharp를 사용하여 특정 DataGridView 열만 PDF 파일에 표시하려고합니다. PDF 문서에 전체 GridView를 삽입 할 수 있다는 것을 알고 있습니다. 그러나 DataGridView의 특정 열만 표시 할 수 있습니까?iTextSharp 특정 DataGridView 열만 표시

 iTextSharp.text.Font fontTable = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK); 

     PdfPTable table = new PdfPTable(dataGridView1.Columns.Count); 
     table.SpacingBefore = 45f; 
     table.TotalWidth = 300; 
     table.DefaultCell.Phrase = new Phrase() { Font = fontTable }; 

     for (int j = 0; j < dataGridView1.Columns.Count; j++) 
     { 
      table.AddCell(new Phrase(dataGridView1.Columns[j].HeaderText, fontTable)); 

     } 

     table.HeaderRows = 1; 

     for (int i = 0; i < dataGridView1.Rows.Count; i++) 
     { 
      for (int k = 0; k < dataGridView1.Columns.Count; k++) 
      { 
       if (dataGridView1[k, i].Value != null) 
       { 
        table.AddCell(new Phrase(dataGridView1[k, i].Value.ToString(),fontTable)); 
       } 
      } 
     } 



     doc.Add(table); 
+0

이 질문은 실제로 iTextSharp 질문되지 않습니다 : 대신에 :

PdfPTable table = new PdfPTable(dataGridView1.Columns.Count); 

당신이 그런 짓을 할 수 있습니다. 당신은 위의 두 가지, 하나는 WinForms이고 하나는 ASP.Net (정확하게 기억한다면)입니다. 어쨌든, iTextSharp에서 이러한 작업을하려면 가운데 단계에서 열을 제거해야한다는 중간 단계가 필요합니다. –

+0

아마도 중간 단계가 도움이 될 것이라고 제안하는 것이 좋습니다 ... – Tom

+0

나는 이미 그 단계를 완료했다고 가정 했었습니다. 기존의 모눈 (여전히 사용중인 것이 확실하지 않음)을 PDF로 복제했으며 _now_ 열을 제한하고 싶습니다. 여러분이 이것을 할 수 있다는 것을 알았다고 말했기 때문에 나는 그 효과에 대한 샘플 코드를 보았고 그것을 구현했다고 생각할 것입니다. 그렇지 않은 경우 .Net 그리드를 PDF로 가져 오는 것이 가장 좋습니다. –

답변

0

내가 지금이 완벽 컴파일하지 않을 수 있습니다하지만 당신은 당신의 for 문에서 어떤 임의의 조건을 쓸 필요가 내 앞에 IDE가없는 :

이 지금까지 내 코드입니다. 이 코드는 링크 된 페이지에서 직접 해제됩니다. 열 이름을 확인하고 건너 뛰는 문장을 일부 추가했습니다 (if).

foreach (DataGridViewColumn c in GridView.Columns) 
{ 
    if(c.Name == "Something") 
    { 
     //Skip this column 
     continue; 
    } 

    //process these columns 
    //.. 
} 

for (int i = 0; i < GridView.Rows.Count - 1; i++) 
{ 
    for (int j = 0; j < GridView.Columns.Count - 1; j++) 
    { 
     if(GridView.Columns[j].Name == "Something") 
     { 
      //Skip this column 
      continue 
     } 

     //Process these columns 
    } 
} 

EDIT 현재 코드를 기반으로

는 위의 같은 것을 원하는 : 당신이 ID을 사용해야 할 수도 있습니다

iTextSharp.text.Font fontTable = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK); 

PdfPTable table = new PdfPTable(dataGridView1.Columns.Count); 
table.SpacingBefore = 45f; 
table.TotalWidth = 300; 
table.DefaultCell.Phrase = new Phrase() { Font = fontTable }; 

for (int j = 0; j < dataGridView1.Columns.Count; j++) 
{ 
    if(dataGridView1.Columns[j].Name == "Something") 
    { 
     continue; 
    } 
    table.AddCell(new Phrase(dataGridView1.Columns[j].HeaderText, fontTable)); 

} 

table.HeaderRows = 1; 

for (int i = 0; i < dataGridView1.Rows.Count; i++) 
{ 
    for (int k = 0; k < dataGridView1.Columns.Count; k++) 
    { 
     if(dataGridView1.Columns[k].Name == "Something") 
     { 
      continue; 
     } 
     if (dataGridView1[k, i].Value != null) 
     { 
      table.AddCell(new Phrase(dataGridView1[k, i].Value.ToString(),fontTable)); 
     } 
    } 
} 
doc.Add(table); 

대신 Name 또는 j 단지 실제 인덱스를 그리고 k은 DGV 설정 방법에 달려 있습니다.

루프를 for 루프 내에서 약간 변경하고 싶을 수도 있습니다. null을 올바르게 확인하고 있지만 여전히 셀을 추가해야합니다. 그렇지 않으면 전체 테이블이 이동할 수 있습니다. 당신이 확인해야합니다

if (dataGridView1[k, i].Value != null) 
{ 
    table.AddCell(new Phrase(dataGridView1[k, i].Value.ToString(),fontTable)); 
} 
else 
{ 
    table.AddCell(new Phrase("")); 
} 

또 하나의 변화는 실제 PdfPTable의 인스턴스이다. iTextSharp 절대적으로 아무 생각이 무엇을`DataGridView` 또는`GridView`이 없기 때문에

PdfPTable table = new PdfPTable(dataGridView1.Columns.Count - 2); //Subtract the number of columns that you are hiding 
+0

열을 "건너 뛰기"위해 필요한 코드는 무엇입니까? – Tom

+0

위의 코드. 'for' 루프에서'continue'가 해당 인덱스를 건너 뜁니다. –

+0

코드를 시도했는데 어떤 열도 표시되지 않습니다. – Tom

관련 문제