This is a migrated thread and some comments may be shown as answers.

Inserting and Updating tables in Richtextbox.document editor

3 Answers 96 Views
RichTextBox (obsolete as of Q3 2014 SP1)
This is a migrated thread and some comments may be shown as answers.
Ramana
Top achievements
Rank 1
Ramana asked on 01 Oct 2013, 06:01 AM
Hi,

I have a scenario where I have to programmatically create Tables in a RichTextbox editor. The tables are created but I want to insert data into these tables. is there some way to programmatically get to blank tables inside a document and add data into its cells.

could you please share the code for the same?


Thanks in Advance,

Ramana

3 Answers, 1 is accepted

Sort by
0
George
Telerik team
answered on 03 Oct 2013, 03:52 PM
Hello Ramana,

Thank you for contacting us.

The best choice in your case would be to save the Table's reference when creating it in a List<Table> or array. This way you can access them at any time. You can then easily insert TableCells in them. Below is a sample method:
private Table CreateTable(RadRichTextBox richTextBoxToModify)
{
    Section section = new Section(); 
    Table table = new Table(1, 1);     
    section.Blocks.Add(table); 
    richTextBoxToModify.Document.Sections.Add(section);
 
    return table;
}

I also recommend you to read about the Element Hierarchy in RadRichTextBox.

If you have any other questions, do not hesitate to ask.

Regards,
George
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Justin
Top achievements
Rank 1
answered on 20 Nov 2013, 08:36 PM

I had a similar issue and after looking around for a while I decided to set up a method that I could use to loop through my "dataset" as needed to insert rows to a newly created table (in this case it is the only table in the doc, but I have another solution using the same method - just with a list of Tables with a reference Id). Since you can create a table without knowing the number of rows/columns that will be needed it is even easier to then add each row with the number of cells you need.

Not sure whether this is the best method - but it is working great for me and goes through the data fairly quickly :)

Below is a sample of what I am doing with a collection of work items retrieved from a TFS Server (I have removed the logic of pulling the fields I need from each item in the List, which, unfortunately, does not Implement IEnumerable so I cannot do a foreach).



for (int i = 0; i < numRows - 1; i++)
{
     table.Rows.Add(AddNewRow(new string[] { string1,string2,string3}));
}




The method for creating a new row takes an array of strings for each column/cell - I am also taking advantage of this time to do some formatting which is specific to my scenario





private TableRow AddNewRow(string[] items)
{
     TableRow newRow = new TableRow();
 
     for (int i = 0; i < items.Length; i++)
     {
          TableCell cell = new TableCell();
          Paragraph paragraph = new Paragraph();
          Span span = new Span();
          var newString = items[i];
           
          if (string.IsNullOrEmpty(items[i]))
               newString = " ? ";
 
          if (i == 1)
          {
               int status = 0;
               int.TryParse(newString, out status);
                
               if (status > 0)
               {
                    Color bgColor = Color.White;
 
                    switch (status)
                    {
                         case 1:
                              bgColor = Color.Green;
                              break;
                         case 2:
                              bgColor = Color.Yellow;
                              break;
                         case 3:
                              bgColor = Color.Red;
                              break;
                    }
 
                    cell.Background = bgColor;
                    span.ForeColor = bgColor;
                }
          }
          span.Text = newString;
          paragraph.Inlines.Add(span);
          cell.Blocks.Add(paragraph);
          newRow.Cells.Add(cell);
     }
 
     return newRow;
}










0
George
Telerik team
answered on 25 Nov 2013, 03:59 PM
Hi Justin,

Thank you for sharing your solution with the community. I have updated your Telerik Points for your time and effort.

Regards,
George
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
RichTextBox (obsolete as of Q3 2014 SP1)
Asked by
Ramana
Top achievements
Rank 1
Answers by
George
Telerik team
Justin
Top achievements
Rank 1
Share this question
or