How to make my SQLite print method dynamic

I am using SQLite to store data for my application. The app has a user interface that loads data from a SQLite database to display a table by table to the user. Basically, the user can click left or right and view other tables one by one.

The user can also click a button that will show a preview of the data and print it. I've got this working, but I'm having some problems designed to dynamically display any table on the print preview screen. My problems are some of the column headers are too long, etc., basically how to calculate the size of each column, etc. Should I scroll and find the maximum character size of any text in the entire column and then set the column width only wider than this, or is there an easier way to do this?

I am also writing the datasheet to a comma separated csv file, so it is better to use the existing solution to print from a csv file, if any of you know such a solution, please suggest it!

Anyway, here's the existing code:

// ------------------------ code that gets called when the print button is clicked ----------------------------

// holds the row data
List<string[]> myList = new List<string[]>();

if(ReportPage == 1)
{
    int rowCount = MyTestTable.RowCount;
    for(int i = 0; i <rowCount; i++)
    {
         MyTestTable.SelectedRowIndex = i;
         var row1 = MyTestTable.GetSelectedRow();
         var cols1 = row1.ItemArray;

        string Col1 = cols1[row1.FindIndexOfColumn("Col1")].ToString();
        string Col2 = cols1[row1.FindIndexOfColumn("Col2")].ToString();
        string Col3 = cols1[row1.FindIndexOfColumn("Col3")].ToString();
        string Col4 = cols1[row1.FindIndexOfColumn("Col4")].ToString();
        string Col5 = cols1[row1.FindIndexOfColumn("Col5")].ToString();
        string Col6 = cols1[row1.FindIndexOfColumn("Col6")].ToString();
        string Col7 = cols1[row1.FindIndexOfColumn("Col7")].ToString();
        myList.Add(new string[] { Col1, Col2, Col3, Col4, Col5, Col6, Col7 });
    }

string[] cols = new string[7];
cols[0] = "Col1";
cols[1] = "Col2";
cols[2] = "Col3";
cols[3] = "Col4";
cols[4] = "Col5";
cols[5] = "Col6";
cols[6] = "Col7";

PrintUtility.SetUpDocument("TEST", cols, myList);


}
PrintUtility.TestNewReport();






// ---------------------- plugin code that gets called from the application 

namespace PrintUtility
{

     public class PrintUtility : UserComponentBase
    {
        public PrintDocument document;
        public DataGridView dataGridView;

        public PrintUtility()
        {
            document = new PrintDocument();
            dataGridView = new DataGridView();
        }



        public void SetUpDocument(string title, string[] cols,  List<string[]> rows)
        {
            document = new PrintDocument();
            dataGridView = new DataGridView();
            document.DocumentName = title;
            document.DefaultPageSettings.Landscape = true;
            document.PrintPage += PrintPage;

            DataGridView dataGrid = new DataGridView();
            dataGrid.ColumnCount = cols.Length;
            for (int i = 0; i < cols.Length; i++ )
            {
                dataGrid.Columns[i].Name = cols[i];
            }

            foreach(string[] row in rows)
            {
                dataGrid.Rows.Add(row);
            }

            this.dataGridView = dataGrid;
            document.DocumentName = title;
            document.PrintPage += PrintPage;
        }


        public PrintDocument GetDocument()
        {
            return this.document;
        }

        private DataTable ConvertListToDataTable(List<string[]> list)
        {
            // New table.
            DataTable table = new DataTable();

            // Get max columns.
            int columns = 0;
            foreach (var array in list)
            {
                if (array.Length > columns)
                {
                    columns = array.Length;
                }
            }

            // Add columns.
            for (int i = 0; i < columns; i++)
            {
                table.Columns.Add();
            }

            // Add rows.
            foreach (var array in list)
            {
                table.Rows.Add(array);
            }

            return table;
        }

        public void TestNewReport()
        {
            Report report = new Report(new PdfFormatter());
            FontDef fd = new FontDef(report, "Helvetica");
            FontProp fp = new FontPropMM(fd, 4);
            FontProp fp_Title = new FontPropMM(fd, 6);
            FontProp fp_Word = new FontPropMM(fd, 3);
            fp_Title.bBold = true;

            List<string> columns = new List<string>();
            foreach (DataGridViewColumn column in dataGridView.Columns)
            {
                columns.Add(column.Name.ToString());
            }

            List<List<string>> rows = new List<List<string>>();
            foreach (DataGridViewRow row in dataGridView.Rows)
            {
                List<string> rowSingle = new List<string>();
                foreach (DataGridViewCell cell in row.Cells)
                {
                    try
                    {
                        rowSingle.Add(cell.Value.ToString());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }
                rows.Add(rowSingle);
            }

            // AUDIT TABLE ( This way of doing things is not dynamic )
            Page page = new Page(report);
            page.SetLandscape();
            int x = 10;
            int y = 40;
            int pastLength = 0;
            foreach(string col in columns)
            {
                x += ((pastLength * 14) + 31);
                page.Add(x, y, new RepString(fp_Title, col));
                pastLength = col.Length;

            }
            page.Add(0, 52, new RepString(fp_Title, "_________________________________________________________________"));

            /* Dynamic way starting
            int rowX = 10;
            int rowY = 105;
            foreach (List<string> row in rows)
            {
                int pastLength2 = 0;
                foreach (string rowItem in row)
                {
                    page.Add(rowX, rowY, new RepString(fp_Word, rowItem));
                    rowX += ((pastLength * 14) + 31);
                    pastLength2 = rowItem.Length;
                }
                rowY += 30;
            }
             */

            fp_Title.rSizeMM = 8;
            int amountRowsPerPageSoFar = 0;
            int rowY = 80;

            foreach (List<string> row in rows)
            {
                try
                {
                    string iDItem = row[0];
                    page.Add(40, rowY, new RepString(fp_Word, iDItem));

                    string typeItem = row[1];
                    page.Add(120, rowY, new RepString(fp_Word, typeItem));

                    string descriptionItem = row[2];
                    page.Add(190, rowY, new RepString(fp_Word, descriptionItem));

                    string timestampItem = row[3];
                    page.Add(375, rowY, new RepString(fp_Word, timestampItem));

                    string userItem = row[4];
                    page.Add(555, rowY, new RepString(fp_Word, userItem));

                    string stationItem = row[5];
                    page.Add(655, rowY, new RepString(fp_Word, stationItem));

                    string activeItem = row[6];
                    page.Add(775, rowY, new RepString(fp_Word, activeItem));
                    amountRowsPerPageSoFar++;

                    rowY += 30;
                    if (amountRowsPerPageSoFar >= 17)
                    {
                        page = new Page(report);
                        page.SetLandscape();
                        amountRowsPerPageSoFar = 0;
                        rowY = 40;
                    }

                }
                catch (Exception ex)
                {

                }
            }

            RT.ViewPDF(report, "TestReport.pdf");
        }

    }
}

      

+3


source to share





All Articles