Beispiel #1
0
        public static void Print_DataGridView(DataGridView dgv1, string title)
        {
            CoolPrintPreviewDialog ppvw;
            try
            {
                // Getting DataGridView object to print
                dgv = dgv1;
                PrintTitle = title;

                // Getting all Coulmns Names in the DataGridView
                AvailableColumns.Clear();
                foreach (DataGridViewColumn c in dgv.Columns)
                {
                    if (!c.Visible) continue;
                    AvailableColumns.Add(c.HeaderText);
                }

                // 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)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {

            }
        }
Beispiel #2
0
        public static void Print_DataGridView(DataGridView dgv1, string title)
        {
            CoolPrintPreviewDialog ppvw;

            try
            {
                // Getting DataGridView object to print
                dgv        = dgv1;
                PrintTitle = title;

                // Getting all Coulmns Names in the DataGridView
                AvailableColumns.Clear();
                foreach (DataGridViewColumn c in dgv.Columns)
                {
                    if (!c.Visible)
                    {
                        continue;
                    }
                    AvailableColumns.Add(c.HeaderText);
                }

                // 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)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
            }
        }