2016-07-05 16 views
-1

私はvb.netプログラミングの初心者です。私はそれが本当に難しいのdatagridviewで選択された列を印刷しているのを見ている。 12の列があり、私はランドスケープビューでほんの少しだけ印刷したいと思います。誰かが関連記事や記事を手助けできますか?vb.netで選択したDatagridview列を印刷するには?

答えて

1

としては答えhere

コード:

private static void PrintDoc_PrintPage(object sender, 
     System.Drawing.Printing.PrintPageEventArgs e) 
{ 
    int tmpWidth, i; 
    int tmpTop = e.MarginBounds.Top; 
    int tmpLeft = e.MarginBounds.Left; 
    int HeaderHeight=0; 

    try 
    {    
     // Before starting first page, it saves 
     // Width & Height of Headers and CoulmnType 
     if (PageNo == 1) 
     { 
      foreach (DataGridViewColumn GridCol in dgv.Columns) 
      { 
       if (!GridCol.Visible) continue; 
       // Skip if the current column not selected 
       if (!PrintDGV.SelectedColumns.Contains(
        GridCol.HeaderText)) continue; 

       // Detemining whether the columns 
       // are fitted to the page or not. 
       if (FitToPageWidth) 
        tmpWidth = (int)(Math.Floor((double)(
           (double)GridCol.Width/
           (double)TotalWidth * (double)TotalWidth * 
           ((double)e.MarginBounds.Width/
           (double)TotalWidth)))); 
       else 
        tmpWidth = GridCol.Width; 

       HeaderHeight = 
        (int)(e.Graphics.MeasureString(GridCol.HeaderText, 
        GridCol.InheritedStyle.Font, tmpWidth).Height) + 11; 

       // Save width & height of headres and ColumnType 
       ColumnLefts.Add(tmpLeft); 
       ColumnWidths.Add(tmpWidth); 
       ColumnTypes.Add(GridCol.GetType()); 
       tmpLeft += tmpWidth; 
      } 
     } 

     // Printing Current Page, Row by Row 
     while (RowPos <= dgv.Rows.Count - 1) 
     { 
      DataGridViewRow GridRow = dgv.Rows[RowPos]; 
      if (GridRow.IsNewRow || (!PrintAllRows && !GridRow.Selected)) 
      { 
       RowPos++; 
       continue; 
      } 

      CellHeight = GridRow.Height; 

      if (tmpTop + CellHeight >= 
       e.MarginBounds.Height + e.MarginBounds.Top) 
      { 
       DrawFooter(e, RowsPerPage); 
       NewPage = true; 
       PageNo++; 
       e.HasMorePages = true; 
       return; 
      } 
      else 
      { 
       if (NewPage) 
       { 
        // Draw Print Title 
        e.Graphics.DrawString(PrintTitle, 
          new Font(dgv.Font, FontStyle.Bold), 
          Brushes.Black, e.MarginBounds.Left, 
          e.MarginBounds.Top - 
          e.Graphics.MeasureString(PrintTitle, 
          new Font(dgv.Font, 
          FontStyle.Bold), 
          e.MarginBounds.Width).Height - 13); 

        String s = DateTime.Now.ToLongDateString() + " " + 
           DateTime.Now.ToShortTimeString(); 
        // Draw Time and Date  
        e.Graphics.DrawString(s, 
          new Font(dgv.Font, FontStyle.Bold), 
          Brushes.Black, e.MarginBounds.Left + 
          (e.MarginBounds.Width - 
          e.Graphics.MeasureString(s, new Font(dgv.Font, 
          FontStyle.Bold), e.MarginBounds.Width).Width), 
          e.MarginBounds.Top - 
          e.Graphics.MeasureString(PrintTitle, 
          new Font(new Font(dgv.Font, 
          FontStyle.Bold), FontStyle.Bold), 
          e.MarginBounds.Width).Height - 13); 

        // Draw Headers 
        tmpTop = e.MarginBounds.Top; 
        i = 0; 
        foreach (DataGridViewColumn GridCol in dgv.Columns) 
        { 
         if (!GridCol.Visible) continue; 
         if (!PrintDGV.SelectedColumns.Contains(
              GridCol.HeaderText)) 
          continue; 

         e.Graphics.FillRectangle(new 
          SolidBrush(Color.LightGray), 
          new Rectangle((int) ColumnLefts[i], tmpTop, 
          (int)ColumnWidths[i], HeaderHeight)); 

         e.Graphics.DrawRectangle(Pens.Black, 
          new Rectangle((int) ColumnLefts[i], tmpTop, 
          (int)ColumnWidths[i], HeaderHeight)); 

         e.Graphics.DrawString(GridCol.HeaderText, 
          GridCol.InheritedStyle.Font, 
          new SolidBrush(GridCol.InheritedStyle.ForeColor), 
          new RectangleF((int)ColumnLefts[i], tmpTop, 
          (int)ColumnWidths[i], HeaderHeight), StrFormat); 
         i++; 
        } 
        NewPage = false; 
        tmpTop += HeaderHeight; 
       } 

       // Draw Columns Contents 
       i = 0; 
       foreach (DataGridViewCell Cel in GridRow.Cells) 
       { 
        if (!Cel.OwningColumn.Visible) continue; 
        if (!SelectedColumns.Contains(
          Cel.OwningColumn.HeaderText)) 
         continue; 

        // For the TextBox Column 
        if (((Type) ColumnTypes[i]).Name == 
         "DataGridViewTextBoxColumn" || 
         ((Type) ColumnTypes[i]).Name == 
         "DataGridViewLinkColumn") 
        { 
         e.Graphics.DrawString(Cel.Value.ToString(), 
           Cel.InheritedStyle.Font, 
           new SolidBrush(Cel.InheritedStyle.ForeColor), 
           new RectangleF((int)ColumnLefts[i], 
           (float)tmpTop, 
           (int)ColumnWidths[i], 
           (float)CellHeight), StrFormat); 
        } 
        // For the Button Column 
        else if (((Type) ColumnTypes[i]).Name == 
            "DataGridViewButtonColumn") 
        { 
         CellButton.Text = Cel.Value.ToString(); 
         CellButton.Size = new Size((int)ColumnWidths[i], 
                CellHeight); 
         Bitmap bmp = new Bitmap(CellButton.Width, 
               CellButton.Height); 
         CellButton.DrawToBitmap(bmp, new Rectangle(0, 0, 
           bmp.Width, bmp.Height)); 
         e.Graphics.DrawImage(bmp, 
          new Point((int)ColumnLefts[i], tmpTop)); 
        } 
        // For the CheckBox Column 
        else if (((Type) ColumnTypes[i]).Name == 
           "DataGridViewCheckBoxColumn") 
        { 
         CellCheckBox.Size = new Size(14, 14); 
         CellCheckBox.Checked = (bool)Cel.Value; 
         Bitmap bmp = new Bitmap((int)ColumnWidths[i], 
               CellHeight); 
         Graphics tmpGraphics = Graphics.FromImage(bmp); 
         tmpGraphics.FillRectangle(Brushes.White, 
           new Rectangle(0, 0, 
           bmp.Width, bmp.Height)); 
         CellCheckBox.DrawToBitmap(bmp, 
           new Rectangle((int)((bmp.Width - 
           CellCheckBox.Width)/2), 
           (int)((bmp.Height - CellCheckBox.Height)/2), 
           CellCheckBox.Width, CellCheckBox.Height)); 
         e.Graphics.DrawImage(bmp, 
           new Point((int)ColumnLefts[i], tmpTop)); 
        } 
        // For the ComboBox Column 
        else if (((Type) ColumnTypes[i]).Name == 
           "DataGridViewComboBoxColumn") 
        { 
         CellComboBox.Size = new Size((int)ColumnWidths[i], 
               CellHeight); 
         Bitmap bmp = new Bitmap(CellComboBox.Width, 
               CellComboBox.Height); 
         CellComboBox.DrawToBitmap(bmp, new Rectangle(0, 0, 
           bmp.Width, bmp.Height)); 
         e.Graphics.DrawImage(bmp, 
           new Point((int)ColumnLefts[i], tmpTop)); 
         e.Graphics.DrawString(Cel.Value.ToString(), 
           Cel.InheritedStyle.Font, 
           new SolidBrush(Cel.InheritedStyle.ForeColor), 
           new RectangleF((int)ColumnLefts[i] + 1, 
           tmpTop, (int)ColumnWidths[i] 
           - 16, CellHeight), StrFormatComboBox); 
        } 
        // For the Image Column 
        else if (((Type) ColumnTypes[i]).Name == 
           "DataGridViewImageColumn") 
        { 
         Rectangle CelSize = new Rectangle((int)ColumnLefts[i], 
           tmpTop, (int)ColumnWidths[i], CellHeight); 
         Size ImgSize = ((Image)(Cel.FormattedValue)).Size; 
         e.Graphics.DrawImage((Image)Cel.FormattedValue, 
           new Rectangle((int)ColumnLefts[i] + 
           (int)((CelSize.Width - ImgSize.Width)/2), 
           tmpTop + (int)((CelSize.Height - 
           ImgSize.Height)/2), 
           ((Image)(Cel.FormattedValue)).Width, 
           ((Image)(Cel.FormattedValue)).Height)); 

        } 

        // Drawing Cells Borders 
        e.Graphics.DrawRectangle(Pens.Black, 
          new Rectangle((int)ColumnLefts[i], 
          tmpTop, (int)ColumnWidths[i], CellHeight)); 

        i++; 

       } 
       tmpTop += CellHeight; 
      } 

      RowPos++; 
      // For the first page it calculates Rows per Page 
      if (PageNo == 1) RowsPerPage++; 
     } 

     if (RowsPerPage == 0) return; 

     // Write Footer (Page Number) 
     DrawFooter(e, RowsPerPage); 

     e.HasMorePages = false; 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message, "Error", 
         MessageBoxButtons.OK, 
         MessageBoxIcon.Error); 
    } 
} 

はまた、以下のリンク参照してください。

https://social.msdn.microsoft.com/Forums/en-US/540b51be-b1d5-4877-87bb-35e90fd95493/printing-a-datagridview-in-vbnet?forum=vbgeneral

http://www.codeproject.com/Articles/16601/Printing-Selected-Columns-and-Rows-in-a-DataGrid

http://www.c-sharpcorner.com/forums/datagridview-printing-by-user-selecting-rows-and-columns

http://www.vbforums.com/showthread.php?523396-RESOLVED-Printing-a-Datagridview-Rows

関連する問題