Datagrid视图打印错误 [英] Datagrid view Printing error

查看:77
本文介绍了Datagrid视图打印错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好...
我已经制作了一个具有打印功能的winform应用程序,该功能可以从datagrid视图中进行打印...但是当我加载页面时,它首先向我显示一个错误,提示"SystemInvalid OperationException.在调用填充之前,尚未初始化select命令属性".然后转到页面,但是令人惊讶的是,当我单击打印预览"时,这一切似乎都有效.

我会给你必要的代码...

我需要的是为我纠正打印问题的人...它说错误在加载页面代码部分中...

代码:

Hello everyone...
I have made a winform application with a printing function which prints from the datagrid view... but when ever i load the page it first shows me an error saying "SystemInvalid OperationException. The select command property has not been initialized before calling the fill" and then it goes to the page but surprisingly when i click print preview it all seems to work.

Iam giving u the necessary codes...

What i need is someone to correct the printing problem for me... it says the error is on the load page code section...

CODE:

using System.Drawing.Printing;

namespace Number_Plate
{
    public partial class Records : Form
    {
        DataSet ds = new DataSet();
        SqlConnection cs = new SqlConnection("Data Source=MAAZA-PC;Initial Catalog=NumberPlates;Integrated Security=True");
        SqlDataAdapter da = new SqlDataAdapter();

        BindingSource TableBinding = new BindingSource();

        DataGridViewPrinter MyDataGridViewPrinter;

        public Records()
        {
            InitializeComponent();
        }
        private void DGUpdate()
        {
            DataGridView.ClearSelection();
            DataGridView.Rows[TableBinding.Position].Selected = true;
        }
        private void records()
        {
            TxtToolStrip.Text = "Records " + TableBinding.Position + " of " + (TableBinding.Count - 1);
        }
        private void BtnDisplay_Click(object sender, EventArgs e)
        {
            da.SelectCommand = new SqlCommand("SELECT * FROM Number", cs);
            ds.Clear();
            da.Fill(ds);

            DataGridView.DataSource = ds.Tables[0];

            TableBinding.DataSource = ds.Tables[0];
            TxtSerial.DataBindings.Add(new Binding("Text", TableBinding,"Serial"));
            TxtRegistrationNumber.DataBindings.Add(new Binding("Text", TableBinding, "RegistrationNumber"));
            TxtSize.DataBindings.Add(new Binding("Text", TableBinding, "Size"));
            TxtColor.DataBindings.Add(new Binding("Text", TableBinding, "Color"));
            TxtOwnerName.DataBindings.Add(new Binding("Text", TableBinding, "OwnerName"));
            TxtAmountPaid.DataBindings.Add(new Binding("Text", TableBinding, "AmountPaid"));
            DateTimePickerPlate.DataBindings.Add(new Binding("Text", TableBinding, "Date"));

            records();
        }
        private void Records_Load(object sender, EventArgs e)
        {
            try
            {
                da.Fill(ds, "dt");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Operation failed: " + ex.ToString(), Application.ProductName + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Setting the style of the DataGridView control
            DataGridView.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 9, FontStyle.Bold, GraphicsUnit.Point);
            DataGridView.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.ControlDark;
            DataGridView.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            DataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            DataGridView.DefaultCellStyle.Font = new Font("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point);
            DataGridView.DefaultCellStyle.BackColor = Color.Empty;
            DataGridView.AlternatingRowsDefaultCellStyle.BackColor = SystemColors.ControlLight;
            DataGridView.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            DataGridView.GridColor = SystemColors.ControlDarkDark;

            // Binding the DataGridViewControl to the DataSet generated above
            DataGridView.DataSource = ds;
            DataGridView.DataMember = "dt";

            // Changing the last column alignment to be in the Right alignment            
            DataGridView.Columns[DataGridView.Columns.Count - 1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            // Adjusting each column to be fit as the content of all its cells, including the header cell
            DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

            //MyDataGridView.Columns[0].Visible = false;
        }

        private void BtnPrint_Click(object sender, EventArgs e)
        {
            if (SetupThePrinting())
                PrintDocument.Print();
        }

        private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            bool more = MyDataGridViewPrinter.DrawDataGridView(e.Graphics);
            if (more == true)
                e.HasMorePages = true;
        }

        private void BtnPrintPreview_Click(object sender, EventArgs e)
        {
            if (SetupThePrinting())
            {
                PrintPreviewDialog.Document = PrintDocument;
                PrintPreviewDialog.ShowDialog();
            }
        }

        private bool SetupThePrinting()
        {
            PrintingDialog.AllowCurrentPage = false;
            PrintingDialog.AllowPrintToFile = false;
            PrintingDialog.AllowSelection = false;
            PrintingDialog.AllowSomePages = false;
            PrintingDialog.PrintToFile = false;
            PrintingDialog.ShowHelp = false;
            PrintingDialog.ShowNetwork = false;

            if (PrintingDialog.ShowDialog() != DialogResult.OK)
                return false;

            PrintDocument.DocumentName = "Number Plate Report";
            PrintDocument.PrinterSettings = PrintingDialog.PrinterSettings;
            PrintDocument.DefaultPageSettings = PrintingDialog.PrinterSettings.DefaultPageSettings;
            PrintDocument.DefaultPageSettings.Margins = new Margins(40, 40, 40, 40);

            if (MessageBox.Show("Do you want the report to be centered on the page", "InvoiceManager - Center on Page", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                MyDataGridViewPrinter = new DataGridViewPrinter(DataGridView, PrintDocument, true, true, "Customers", new Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, true);
            else
                MyDataGridViewPrinter = new DataGridViewPrinter(DataGridView, PrintDocument, false, true, "Customers", new Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, true);

            return true;
        }
    }
}

推荐答案

检查这些博客
通过选择列和行进行DataGridView打印 [另一台DataGridView打印机 [
check these blogs
DataGridView Printing by Selecting Columns and Rows[^]
Another DataGridView Printer[^]
--NDK




请遵循以下示例:

在C#.NET 2.0中打印数据网格视图 [
Hi,

Follow this example:

Printing a datagridview in C# .NET 2.0[^]


尝试此URL ... ........
http://www.c-sharpcorner.com/UploadFile/mgold/ExerciseTracker02272006010240AM/ExerciseTracker.aspx [ ^ ]
Try this url................
http://www.c-sharpcorner.com/UploadFile/mgold/ExerciseTracker02272006010240AM/ExerciseTracker.aspx[^]


这篇关于Datagrid视图打印错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆