public static void Print_GridView(GridView dgv1, string title) { CoolPrintPreviewDialog ppvw; try { // Getting GridView object to print dgv = dgv1; PrintTitle = title; // Getting all Coulmns Names in the GridView AvailableColumns.Clear(); foreach (GridColumn c in dgv.Columns) { if (!c.Visible) { continue; } AvailableColumns.Add(c.Caption); } // Showing the PrintOption Form PrintOptions dlg = new PrintOptions(AvailableColumns); //加载注册表中的内容 string itemNameInRegister = dgv.GetType().GUID.ToString(); string checkItems = RegistryHelper.GetValue(itemNameInRegister); if (!string.IsNullOrEmpty(checkItems)) { string[] items = checkItems.Split(','); dlg.SetCheckedItems(items); } dlg.PrintTitle = PrintTitle;//先赋值给对话框 if (dlg.ShowDialog() != DialogResult.OK) { return; } //保存选项内容 string selectedString = ""; List <string> selectedItems = dlg.GetCheckItems(); foreach (string item in selectedItems) { selectedString += string.Format("{0},", item); } selectedString = selectedString.Trim(','); RegistryHelper.SaveValue(itemNameInRegister, selectedString); PrintTitle = dlg.PrintTitle; PrintAllRows = dlg.PrintAllRows; FitToPageWidth = dlg.FitToPageWidth; SelectedColumns = dlg.GetSelectedColumns(); RowsPerPage = 0; ppvw = new CoolPrintPreviewDialog(); ppvw.Document = printDoc; // Showing the Print Preview Page printDoc.BeginPrint += new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint); printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage); if (ppvw.ShowDialog() != DialogResult.OK) { printDoc.BeginPrint -= new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint); printDoc.PrintPage -= new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage); return; } // Printing the Documnet printDoc.Print(); printDoc.BeginPrint -= new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint); printDoc.PrintPage -= new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage); } catch (Exception ex) { MessageDxUtil.ShowError(ex.Message); } finally { } }
public static void Print_DataGridView(DataGridView dgv1, string title) { try { try { dataGridView_0 = dgv1; string_0 = title; list_1.Clear(); foreach (DataGridViewColumn column in dataGridView_0.Columns) { if (column.Visible) { list_1.Add(column.HeaderText); } } PrintOptions options = new PrintOptions(list_1); string key = dataGridView_0.GetType().GUID.ToString(); string str2 = RegistryHelper.GetValue(key); if (!string.IsNullOrEmpty(str2)) { string[] items = str2.Split(new char[] { ',' }); options.SetCheckedItems(items); } options.PrintTitle = string_0; if (options.ShowDialog() == DialogResult.OK) { string str3 = ""; foreach (string str4 in options.GetCheckItems()) { str3 = str3 + string.Format("{0},", str4); } str3 = str3.Trim(new char[] { ',' }); RegistryHelper.SaveValue(key, str3); string_0 = options.PrintTitle; bool_1 = options.PrintAllRows; bool_2 = options.FitToPageWidth; list_0 = options.GetSelectedColumns(); int_4 = 0; CoolPrintPreviewDialog dialog = new CoolPrintPreviewDialog { Document = printDocument_0 }; printDocument_0.BeginPrint += new PrintEventHandler(PrintDGV.smethod_0); printDocument_0.PrintPage += new PrintPageEventHandler(PrintDGV.smethod_1); if (dialog.ShowDialog() != DialogResult.OK) { printDocument_0.BeginPrint -= new PrintEventHandler(PrintDGV.smethod_0); printDocument_0.PrintPage -= new PrintPageEventHandler(PrintDGV.smethod_1); } else { printDocument_0.Print(); printDocument_0.BeginPrint -= new PrintEventHandler(PrintDGV.smethod_0); printDocument_0.PrintPage -= new PrintPageEventHandler(PrintDGV.smethod_1); } } } catch (Exception exception) { MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand); } } finally { } }