Bind Data to RadRichTextBox

6 posts, 0 answers
  1. Alan
    Alan avatar
    8 posts
    Member since:
    Sep 2011

    Posted 17 Jan 2012 Link to this post

    Currently I am trying to bind data to a richtextbox and I am not getting any data populating.
    I am currently using this to bind the data.
    radRichTextBox1.DataBindings.Add("Text",ds,"Table.ColumnName",false,DataSourceUpdateMode.OnPropertyChanged);

    I have verified I am only pulling one record..
    I have done this with TextBoxes with no issue.
    I have the feeling "Text" should be something else but I am unsure what.
    Any help would be appreciated.
  2. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 20 Jan 2012 Link to this post

    Hi Alan,

    This is not a built-in functionality in the current implementation of RadRichTextBox. To provide this behavior, you can use TxtFormatProvider implementing the Binder component. Here is a solution using binding to a simple TextBox:

    namespace Lab
    {
        public partial class RichTextBoxForm1 : Form
        {
            public RichTextBoxForm1()
            {
                InitializeComponent();
                
                RadRichTextBinder binder = new RadRichTextBinder(this.radRichTextBox1);
                this.textBox1.DataBindings.Add("Text", binder, "Text", true, DataSourceUpdateMode.OnPropertyChanged);
            }
        }
     
        public class RadRichTextBinder : TxtFormatProvider
        {
            private RadRichTextBox richTextBox;
            public event EventHandler TextChanged;
     
            public RadRichTextBinder(RadRichTextBox richTextBox)
            {
                this.richTextBox = richTextBox;
                this.richTextBox.DocumentContentChanged += new EventHandler(richTextBox_DocumentContentChanged);
                 
            }
     
            void richTextBox_DocumentContentChanged(object sender, EventArgs e)
            {
                OnTextChanged(new EventArgs());
            }
     
            protected virtual void OnTextChanged(EventArgs e)
            {
                if (this.TextChanged != null)
                {
                    this.TextChanged(this, e);
                }
            }
     
            public string Text
            {
                get
                {
                    return this.Export(this.richTextBox.Document);
                }
                set
                {
                    this.richTextBox.DocumentContentChanged -= new EventHandler(richTextBox_DocumentContentChanged);
                    this.richTextBox.Document = this.Import(value);
                    OnTextChanged(new EventArgs());
                    this.richTextBox.DocumentContentChanged += new EventHandler(richTextBox_DocumentContentChanged);
                }
            }
        }
    }

    I hope this helps.

    Kind regards,
    Julian Benkov
    the Telerik team

    SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Alan
    Alan avatar
    8 posts
    Member since:
    Sep 2011

    Posted 20 Jan 2012 Link to this post

    This takes care of the problem I was having but presents another problem... I am currently using using databinding from a grid to text fields to the text fields reflect what row is selected in the grid. The solution given handles the issue but does not bind the data in the grid to the data in the textbox, it only places the data in the grid in the textbox.
  5. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 25 Jan 2012 Link to this post

    Hello Alan,

    I would kindly ask you to provide me with additional details about your scenario. It will be best if you can send us a sample project where we can reproduce the issue. This will allow us to understand the exact behavior and the circumstances in which it occurs. Thank you in advance.

    I am looking forward to your reply.

    Greetings,
    Julian Benkov
    the Telerik team

    SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

  6. Alan
    Alan avatar
    8 posts
    Member since:
    Sep 2011

    Posted 01 Feb 2012 Link to this post

    disregard my previous message.
    I do have one other question, what would be involved to convert this binder to work with html so a formatted text string could be bought in and moved out of the richtextboxes.
  7. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 06 Feb 2012 Link to this post

    Hello Alan,

    You can change the base class of the binder to be HtmlFormatProvider and use this new html based binding component, but please keep in mind that this will decrease the performance of your application.

    Do not hesitate to contact us if you have further questions or issues. 

    Kind regards,
    Julian Benkov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready