Answers for "RichTextBox Tables"

0

RichTextBox Tables

int rows = 4;
int cols = 5;
int[,] matrix = new int[rows, cols];
Random rnd = new Random();
for (int i = 0; i < rows; i++)
    for (int j = 0; j < cols; j++)
        matrix[i, j] = rnd.Next(0, 11);
Table table = new Table();
for (int i = 0; i < cols; i++)
    table.Columns.Add(new TableColumn());
TableRowGroup group = new TableRowGroup();
table.RowGroups.Add(group);
for (int i = 0; i < rows; i++)
{
    TableRow row = new TableRow();
    for (int j = 0; j < cols; j++)
    {
        TableCell cell = new TableCell(
            new Paragraph(new Run(matrix[i, j].ToString())));
        cell.BorderBrush = System.Windows.Media.Brushes.Black;
        cell.BorderThickness = new System.Windows.Thickness(5);
        row.Cells.Add(cell);
    }
    group.Rows.Add(row);
}
FlowDocument document = new FlowDocument(table);
TextRange range = new TextRange(document.ContentStart, document.ContentEnd);
MemoryStream stream = new MemoryStream();
range.Save(stream, DataFormats.Rtf);
string rtfTable = Encoding.UTF8.GetString(stream.ToArray());
richTextBox1.Rtf = rtfTable;
Posted by: Guest on July-03-2021

Browse Popular Code Answers by Language