background
I am developing a Windows Forms "Framework 4.0" type application which allows the preview (before printing) of certain data.
The data in question is divided as follows:
- Informative text - separated in 3 lines
- line breaks
- Data from a
DataGridView
.
This is an example of the information I want to print (using the class PrintDocument
) :
MI RESTAURANTE S.A.S.
CALLE 123 # 11-12
85544792
BOGOTA, CUNDINAMARCA 123
*** REPORTE DE LOS MODIFICADORES ***
de 19/01/2018 a 19/02/2018
Modificador Cantidad Valor
Pollo Agridulce 6 0,00
Cerdo Agridulce 28 0,00
No 1 3 0,00
No 2 3 0,00
No 3 7 0,00
No 4 8 0,00
3/4 2 0,00
Bien 1 0,00
Sin 14 0,00
Camaron 13 0,00
Sin Raices 5 0,00
Sin Cebollin 3 0,00
Cebolla 2 0,00
Aceite Oliva 5 0,00
Con 5 0,00
I am using it PrintPreviewDialog
for the preview of the data to be printed.
Problem
When previewing the information, it "despite being tabulated correctly" is not shown organized.
I tried:
- Use
SelectAll()
yClipboard.SetText
to get all the information fromDataGridView
- as shown in this answer . - Loop through
DataGridView
to try to build a table (using characters like "=" or "|" as delimiters) to set a fixed data size. - Determine the number of characters in each cell to determine whether or not to add spaces (emulating the tabulation) , but the preview code applies to more than one report and they can have many lines and a different number of columns.
This is an example of how I currently perform the tabulation of the data obtained from the DataGridView
:
// Filas que tendrá el archivo. - "tbl" es el DataSource del DataGridView:
sstring vlrTableCell = "";
for (int row = 0; row < tbl.Rows.Count; row++)
{
// Asignar el valor del arreglo de items a la variable local.
vlrTableCell = string.Join("\t", tbl.Rows[row].ItemArray);
// ...
}
All attempts result in values not being displayed correctly tabulated or arranged (in preview) .
What I would like to get is a structure similar to this (in the preview) :
MI RESTAURANTE S.A.S.
CALLE 123 # 11-12
85544792
BOGOTA, CUNDINAMARCA 123
*** REPORTE DE LOS MODIFICADORES ***
de 19/01/2018 a 19/02/2018
Modificador Cantidad Valor
Pollo Agridulce 6 0,00
Cerdo Agridulce 28 0,00
No 1 3 0,00
No 2 3 0,00
No 3 7 0,00
No 4 8 0,00
3/4 2 0,00
Bien 1 0,00
Sin 14 0,00
Camaron 13 0,00
Sin Raices 5 0,00
Sin Cebollin 3 0,00
Cebolla 2 0,00
Aceite Oliva 5 0,00
Con 5 0,00
An alternative would be to use the ReportViewer, but I wouldn't know how to use this control to achieve a similar result.
What can I use or how can I get the values of DataGridView
and that they are correctly tabulated (that is, that they are shown correctly tabulated in the data preview) ?
A valid solution for this case was to use the PadRight method .
I modified my code to generate a .txt file with the information stored in a variable of type
DataTable
:By setting the number of spaces for each column to "in this case, I left 15 spaces set", I was able to get a result similar to the expected result in the question.
If the information varies in length, a different fix must be established for each situation; but, PadRight and PadLeft are very useful for this purpose.