Click here to Skip to main content
15,912,493 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
        }
    }
}
Posted

 
Share this answer
 
Hi,

Follow this example:

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900