Click here to Skip to main content
15,885,856 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Problem:
When I have created and pasted the table inside of RichTextBox. Suddently I need to remove or add a row in the table that is located in RichTextBox. How should I do it?


XAML

HTML
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <RichTextBox x:Name="newRtb" />
    </Grid>
</Window>




C#

C#
private void Window_Loaded(object sender, RoutedEventArgs e)
{
   var tab = new Table();
   var gridLenghtConvertor = new GridLengthConverter();

   tab.Columns.Add(new TableColumn() { Name = "Column1", Width = (GridLength)gridLenghtConvertor.ConvertFromString("*") });
   tab.Columns.Add(new TableColumn() { Name = "Column2", Width = (GridLength)gridLenghtConvertor.ConvertFromString("*") });

   tab.RowGroups.Add(new TableRowGroup());

   for(int i=0;i<10;i++)
   {
      tab.RowGroups[0].Rows.Add(new TableRow());
      var tabRow = tab.RowGroups[0].Rows[i];

      tabRow.Cells.Add(new TableCell(new Paragraph(new Run("Row" + (i + 1).ToString() + " Column1"))) { TextAlignment = TextAlignment.Center });
      tabRow.Cells.Add(new TableCell(new Paragraph(new Run("Row" + (i + 1).ToString() + " Column2"))));
   }
   
   newRtb.Document.Blocks.Add(tab);
}
Posted
Updated 18-May-14 10:59am
v2
Comments
Vedat Ozan Oner 18-May-14 16:59pm    
I couldn't understand what you are trying to do.
karthik Udhayakumar 18-May-14 17:00pm    
Suddenly means?during a click event?Not clear can you pls explain:)

1 solution

<pre>private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var tab = new Table();
            tab.Name = "MyTable";
            newRtb.RegisterName("MyTable", tab);


            var gridLenghtConvertor = new GridLengthConverter();

            tab.Columns.Add(new TableColumn() { Name = "Column1", Width = (GridLength)gridLenghtConvertor.ConvertFromString("*") });
            tab.Columns.Add(new TableColumn() { Name = "Column2", Width = (GridLength)gridLenghtConvertor.ConvertFromString("*") });

            tab.RowGroups.Add(new TableRowGroup());

            for (int i = 0; i < 10; i++)
            {
                tab.RowGroups[0].Rows.Add(new TableRow());
                var tabRow = tab.RowGroups[0].Rows[i];

                tabRow.Cells.Add(new TableCell(new Paragraph(new Run("Row" + (i + 1).ToString() + " Column1"))) { TextAlignment = TextAlignment.Center });
                tabRow.Cells.Add(new TableCell(new Paragraph(new Run("Row" + (i + 1).ToString() + " Column2"))));
            }


            newRtb.Document.Blocks.Add(tab);

        }






Table tab  = newRtb.FindName("MyTable") as Table;

            //add row:
            tab.RowGroups[0].Rows.Add(new TableRow());
            var tabRow = tab.RowGroups[0].Rows[tab.RowGroups[0].Rows.Count - 1];

            tabRow.Cells.Add(new TableCell(new Paragraph(new Run("NEW ROW C1"))) { TextAlignment = TextAlignment.Center });
            tabRow.Cells.Add(new TableCell(new Paragraph(new Run("NEW ROW C2"))));

            //remove first row:
            int index = 0;
            tab.RowGroups[0].Rows.RemoveAt(index);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900