Der bliver investeret massivt i AI. Teknologien er mere tilgængelig end nogensinde, og ambitionerne er høje. Alligevel oplever mange virksomheder, at resultaterne udebliver.
Noget ala det her... Det er copy pastet fra noget gammelt kode jeg havde til at ligge. Så det kan godt være der er et par små fejl. Men det burde give dig en ide om, hvordan der kan gøres.
Det er fordi datagridviewprint ikke er med i .Net frameworket, det havde jeg lige glemt i farten. Jeg har downloadet en dll, fra noget der hedder Rustemsoft. Der giver en masse ekstra muligheder med datagridview kontrollen. Man kan så vidt jeg husker downloade en trial af den også, hvor print funktionen også virker. Ellers skal du smide dine data fra dit dataset ind i nogle forskellige arrays, og så bruge GDI+ til at printe det ud med, men der bliver en del kode på den måde... Fandt lige ved at google, et eksempel fra codeproject hvor han printer fra datagridview ved hjælp af GDI+... http://www.codeproject.com/csharp/PrintDataGridView.asp
Det er lige til at copy paste. Det er taget fra det link fra codeproject, men der er lige blevet ændret lidt i det. Jeg har dog fundet et par bugs i koden, men det skulle kun omhandle hvis ens datagridview indeholder store billeder. Men ellers virker det fint... Lav en ny class og tilføj til dit projekt, copy paste så koden her. Så kan du kalde den statiske metode, som er overloaded med en datagridview.
using System; using System.Collections.Generic; using System.Windows.Forms; using System.Drawing; using System.Collections; using System.Data; using System.Text;
namespace ProjektNamespace { class PrintDGV { private static StringFormat StrFormat; // Holds content of a TextBox Cell to write by DrawString private static StringFormat StrFormatComboBox; // Holds content of a Boolean Cell to write by DrawImage private static Button CellButton; // Holds the Contents of Button Cell private static CheckBox CellCheckBox; // Holds the Contents of CheckBox Cell private static ComboBox CellComboBox; // Holds the Contents of ComboBox Cell
private static int TotalWidth; // Summation of Columns widths private static int RowPos; // Position of currently printing row private static bool NewPage; // Indicates if a new page reached private static int PageNo; // Number of pages to print private static ArrayList ColumnLefts = new ArrayList(); // Left Coordinate of Columns private static ArrayList ColumnWidths = new ArrayList(); // Width of Columns private static ArrayList ColumnTypes = new ArrayList(); // DataType of Columns private static int CellHeight; // Height of DataGrid Cell private static int RowsPerPage; // Number of Rows per Page private static System.Drawing.Printing.PrintDocument printDoc = new System.Drawing.Printing.PrintDocument(); // PrintDocumnet Object used for printing
private static string PrintTitle = "PrintTitle"; // Header of pages private static DataGridView dgv; // Holds DataGridView Object to print its contents private static List<string> SelectedColumns = new List<string>(); // The Columns Selected by user to print. private static List<string> AvailableColumns = new List<string>(); // All Columns avaiable in DataGrid private static bool PrintAllRows = true; // True = print all rows, False = print selected rows private static bool FitToPageWidth = true; // True = Fits selected columns to page width , False = Print columns as showed private static int HeaderHeight = 0;
public static void Print_DataGridView(DataGridView dgv1) { PrintPreviewDialog ppvw; try { // Getting DataGridView object to print dgv = dgv1;
// Getting all Coulmns Names in the DataGridView AvailableColumns.Clear(); foreach (DataGridViewColumn c in dgv.Columns) { if (!c.Visible) continue; AvailableColumns.Add(c.HeaderText); }
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;
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 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;
String s = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();
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 Columns 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++; }
Hvis nogen skulle være interesseret i en udgave, hvor det også virker med store billeder, må i lige skrive. Da jeg selv skal bruge det, der går bare lige lidt inden jeg får tid til at lave det. Men håber du får det til at virke
Sad selv med et behov for en god måde at printe mig DG ud på.. Tusinde mange tak for hjælpen!! Ville bare lige lade dig vide at du har fået givet mig en stor hjælp ! :)
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.