Updating Table Cell In code Locks the Table

1 Answer 51 Views
RichTextBox
Steve
Top achievements
Rank 2
Iron
Iron
Iron
Steve asked on 08 Sep 2024, 08:07 AM

Hi,

When I update a table cell in a richtextbox the table is locked and I am unable to edit the table  in the UI. The change to the table cell is not shown in the

UI If I then close the current document and re-open it its fine and I can see the change made to the table cell, or if I make a change to a paragraph above the table from within the UI the change to the cell is shown, and the table is unlocked. 


        public void SetTemplateFromElement(string tagValue, string newValue)
        {
            try
            {
//                RadDocument document = doc_text.Document;
                var table = doc_text.Document.EnumerateChildrenOfType<Table>().Where(x => x.Tag == tagValue).FirstOrDefault();
                if (table != null)
                {

                    switch (tagValue)
                    {
                        case "1000":
                            var row = table.EnumerateChildrenOfType<TableRow>().FirstOrDefault();
                            var cell = row.Cells.ToList()[1];
                            cell.Blocks.Clear();
                            Paragraph p = new Paragraph();
                            Span s = new Span();
                            s.FontWeight = FontWeights.Bold;
                            s.FontFamily = new FontFamily(CGlobals.docu_default_font);
                            s.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);
                            s.Text = newValue; ;
                            p.Inlines.Add(s);
                            cell.Blocks.Add(p);
                            break;
                        case "4000":
                            break;
                    }

                    //                    doc_text.Document = document;
                    doc_text.UpdateLayout();
                    SaveContent(strCurrentDoc);
                }
            }
            catch { }

        }

Can someone please advise.

1 Answer, 1 is accepted

Sort by
1
Accepted
Dimitar
Telerik team
answered on 09 Sep 2024, 06:54 AM

Hi Steve,

We recommend using RadDocumentEditor instead of directly manipulating the document. Here is an example that will achieve a similar result using it (no need to update the layout in this case):

var table = radRichTextBox.Document.EnumerateChildrenOfType<Table>().FirstOrDefault();
var row = table.EnumerateChildrenOfType<TableRow>().FirstOrDefault();
var cell = row.Cells.ToList()[1]; 

RadDocumentEditor editor = new RadDocumentEditor(this.radRichTextBox.Document);
this.radRichTextBox.Document.CaretPosition.MoveToDocumentElementEnd(cell);
editor.ChangeFontFamily(new FontFamily("Consolas"));
editor.ChangeFontSize(Unit.PointToDip(16));
editor.ChangeFontWeight(FontWeights.Bold);
editor.Insert("newValue;");

Regards,
Dimitar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Steve
Top achievements
Rank 2
Iron
Iron
Iron
commented on 09 Sep 2024, 01:45 PM | edited

Hi Dimitar,

Thank you for your answer and supplying the code. It has worked with a minor modification, I had to locate the Span in the TableCell, set it to " ", then delete it using the RadDocumentEditor Delete, otherwise it appended the newValue to the cell.


        public void SetTemplateFromElement(string tagValue, string newValue)
        {
            try
            {
                RadDocumentEditor document = new RadDocumentEditor(doc_text.Document);
                var table = doc_text.Document.EnumerateChildrenOfType<Table>().Where(x => x.Tag == tagValue).FirstOrDefault();
                if (table != null)
                {

                    switch (tagValue)
                    {
                        case "1000":
                            var row = table.EnumerateChildrenOfType<TableRow>().FirstOrDefault();
                            var cell = row.Cells.ToList()[1];
                            
                            var span = cell.EnumerateChildrenOfType<Span>().FirstOrDefault();
                            if(span != null)
                                span.Text = " ";
                            doc_text.Document.CaretPosition.MoveToDocumentElementStart(cell);
                            if(span != null)
                                document.Delete(false);
                            document.ChangeFontFamily(new FontFamily(CGlobals.docu_default_font));
                            document.ChangeFontSize(Unit.PointToDip(CGlobals.docu_default_font_size));
                            document.ChangeFontWeight(FontWeights.Bold);
                            document.Insert(newValue);
                            break;
                        case "4000":
                            break;
                    }
                    //doc_text.Document = document;
                    //doc_text.UpdateLayout();
                    SaveContent(strCurrentDoc);
                }
            }
            catch { }

        }

Dimitar
Telerik team
commented on 10 Sep 2024, 04:54 AM

Hi Steve, 

I am glad this works now. Thank you for sharing it with the community.

Tags
RichTextBox
Asked by
Steve
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Dimitar
Telerik team
Share this question
or