LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Printing a datagrid/dataset in c# (https://www.linuxquestions.org/questions/programming-9/printing-a-datagrid-dataset-in-c-420057/)

mrobertson 02-27-2006 10:01 AM

Printing a datagrid/dataset in c#
 
I have the following code in c#:
Code:

void SetupGridPrinter()
dataGridPrinter1 = new DataGridPrinter(dgQueryResults, printDocument1, Dataset1);
                }
                private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
                {
                        Graphics g = e.Graphics;
                        // Draw  a label title for the grid
                        DrawTopLabel(g);

                        // draw the datagrid using the DrawDataGrid method passing the Graphics surface
                        bool more = dataGridPrinter1.DrawDataGrid(g);

                        // if there are more pages, set the flag to cause the form to trigger another print page event
                        if (more == true)
                        {
                                e.HasMorePages = true;
                                dataGridPrinter1.PageNumber++;
                        }
                }

                private void btnPrint_Click(object sender, System.EventArgs e)
                {
                        // Initialize the datagrid page and row properties
                        dataGridPrinter1.PageNumber = 1;
                        dataGridPrinter1.RowCount = 0;

                        // Show the Print Dialog to set properties and print the document after ok is pressed.
                        if (printDialog1.ShowDialog() == DialogResult.OK)
                        {
                                printDocument1.Print();
                        }


                }

                void DrawTopLabel(Graphics g)
                {
                        int TopMargin = printDocument1.DefaultPageSettings.Margins.Top;

                        //g.FillRectangle(new SolidBrush(label1.BackColor), label1.Location.X, label1.Location.Y + TopMargin, label1.Size.Width, label1.Size.Height);
                        //g.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), label1.Location.X + 50, label1.Location.Y + TopMargin, new StringFormat());
                }

I am getting an error on the boldered area of code stating that I cannot conert a dataset to a data table. What needs to be done to fix this?

tekkieman 02-27-2006 03:43 PM

The dataGridPrinter object wants a datatable. You passed a dataset. An implicit cast is not allowed, although they are not really the same things anyway. The dataset can contain datatables. Does dataGridPrinter really take a datagrid object as a parameter? If it does, passit the correct datatable from the dataset object such as:

Instead of "Dataset1", use "Dataset1.Tables["tableName"];


All times are GMT -5. The time now is 04:57 AM.