我想在Richtextbox上打印表格时显示“ ID”,“ Name”等列名称,并且还想要表格格式

时间:2018-11-29 11:06:05

标签: c# wpf sqlite

    This is my code to add data from datatable to richtextbox on table format
 dt = new DataTable();
                        SQLiteConnection conn = new SQLiteConnection("Data Source=" + folderPath + "\\" + databaseName);
                        InitializeComponent();
                        s = MyProperty.ToString();
                        conn.Open();
                        SQLiteCommand command = new SQLiteCommand("SELECT id,nameSearch,dt,totalRecords FROM ACCESSDETAILS Where id=" + s + " AND numberSearch IS NULL AND dt BETWEEN '" + datepicker + "' AND '" +  datepicker1+ "'", conn);
                        SQLiteDataAdapter da = new SQLiteDataAdapter(command);
                        da.Fill(dt);                        
                        conn.Close() 
var tab = new Table();

                            var gridLenghtConvertor = new GridLengthConverter();
                            tab.Columns.Add(new TableColumn() { Name = "ID", IsEnabled = true, Width = (GridLength)gridLenghtConvertor.ConvertFromString("30") });
                            tab.Columns.Add(new TableColumn() { Name = "Name", IsEnabled = true, Width = (GridLength)gridLenghtConvertor.ConvertFromString("80") });
                            tab.Columns.Add(new TableColumn() { Name = "Date", IsEnabled = true, Width = (GridLength)gridLenghtConvertor.ConvertFromString("100") });
                            tab.Columns.Add(new TableColumn() { Name = "TotalRecords", IsEnabled = true, Width = (GridLength)gridLenghtConvertor.ConvertFromString("auto") });



                            tab.RowGroups.Add(new TableRowGroup());
                            int i = 0;
                            foreach (DataRow dr in dt.Rows)
                            {
                                tab.RowGroups[0].Rows.Add(new TableRow());
                                var tabRow = tab.RowGroups[0].Rows[i];
                                tabRow.Cells.Add(new TableCell(new Paragraph(new Run(dr.ItemArray[0].ToString() + " | "))));
                                tabRow.Cells.Add(new TableCell(new Paragraph(new Run(dr.ItemArray[1].ToString() + " | "))));
                                tabRow.Cells.Add(new TableCell(new Paragraph(new Run(dr.ItemArray[2].ToString() + " | "))));
                                tabRow.Cells.Add(new TableCell(new Paragraph(new Run(dr.ItemArray[3].ToString()))));
                                i++;
                            }
                            rtf.Document.Blocks.Add(tab);

我在这里添加列和列,但该名称无法显示。enter image description here

此图像是我的打印页面,我希望在该页面上将id,名称,日期和总记录作为列名。我想像下面的图像一样围绕它的边框 enter image description here

请帮助我。

1 个答案:

答案 0 :(得分:0)

创建数据表时可以生成列。请参考以下代码作为您的问题的参考。

//Create datatable
 DataTable dt = new DataTable();

//put Your columns Name
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Date", typeof(string));
dt.Columns.Add("Total Records", typeof(string));

//Your Query Code
 SQLiteConnection conn = new SQLiteConnection("Data Source=" + folderPath + "\\" + databaseName);
                        InitializeComponent();
                        s = MyProperty.ToString();
                        conn.Open();
                        SQLiteCommand command = new SQLiteCommand("SELECT id,nameSearch,dt,totalRecords FROM ACCESSDETAILS Where id=" + s + " AND numberSearch IS NULL AND dt BETWEEN '" + datepicker + "' AND '" +  datepicker1+ "'", conn);
                        SQLiteDataAdapter da = new SQLiteDataAdapter(command);
                         ds = new DataSet();
                         da.Fill(ds);

#Fill Dataset values in Datatable rows   
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr[0] =Convert.ToInt32( ds.Tables[0].Rows[i]["id"]);
                        dr[1] = ds.Tables[0].Rows[i]["namesearch"].ToString() + " " + ds.Tables[0].Rows[i]["lastname"].ToString(); 
                        dr[2] = ds.Tables[0].Rows[i]["dt"].ToString();
                        dr[3] = ds.Tables[0].Rows[i]["totalRecords"].ToString();
                        dt.Rows.Add(dr);
                    }                     
                        conn.Close() ;

#For Print Datatable

            PrintDialog printDialog = new PrintDialog();
            if (printDialog.ShowDialog() == true)
            {
                FlowDocument fd = new FlowDocument();

                Table table = new Table();
                TableRowGroup tableRowGroup = new TableRowGroup();
                TableRow r = new TableRow();
                fd.PageWidth = printDialog.PrintableAreaWidth;
                fd.PageHeight = printDialog.PrintableAreaHeight;
                fd.BringIntoView();

                fd.TextAlignment = TextAlignment.Center;
                fd.ColumnWidth = 500;
                table.CellSpacing = 0;
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    r.Cells.Add(new TableCell(new System.Windows.Documents.Paragraph(new Run(dt.Columns[j].ToString()))));
                    r.Cells[j].ColumnSpan = 4;
                    r.Cells[j].Padding = new Thickness(4);
                    r.Cells[j].BorderBrush = Brushes.Black;
                    r.Cells[j].FontWeight = FontWeights.Bold;
                    r.Cells[j].Background = Brushes.DarkGray;
                    r.Cells[j].Foreground = Brushes.White;
                    r.Cells[j].BorderThickness = new Thickness(1, 1, 1, 1);
                }
                tableRowGroup.Rows.Add(r);
                table.RowGroups.Add(tableRowGroup);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var gv = dt.Rows[i];
                    tableRowGroup = new TableRowGroup();
                    r = new TableRow();
                    for (int j = 0; j < gv.ItemArray.Length; j++)
                    {
                        r.Cells.Add(new TableCell(new System.Windows.Documents.Paragraph(new Run(gv.ItemArray[j].ToString()))));
                        r.Cells[j].ColumnSpan = 4;
                        r.Cells[j].Padding = new Thickness(4);
                        r.Cells[j].BorderBrush = Brushes.Black;
                        r.Cells[j].Background = Brushes.White;
                        r.Cells[j].Foreground = Brushes.Black;
                        r.Cells[j].BorderThickness = new Thickness(1, 1, 1, 1);
                    }
                    tableRowGroup.Rows.Add(r);
                    table.RowGroups.Add(tableRowGroup);
                }

                fd.Blocks.Add(table);

                printDialog.PrintDocument(((IDocumentPaginatorSource) fd).DocumentPaginator, "");

            }
        }