Databinding to datatable

2 posts, 0 answers
  1. Peter
    Peter avatar
    4 posts
    Member since:
    Apr 2013

    Posted 12 Feb 2014 Link to this post

    I have some questions about the RadRichTextBox.

    How do I data bind the content of the RadRichTextBox to a datatable, and how could this be accomplished?

    What is the most appropriate field type in an sql database for the content to be stored in?

    If I cannot data bind to a datatable then how can this functionality be created?

    Thanks in advance
  2. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 14 Feb 2014 Link to this post

    Hello Peter,

    I am copying the answer from your support ticket here so the community can benefit from it:

    You can implement a custom TxtFormatProvider using the Import functionality as it is demonstrated in the following forum thread http://www.telerik.com/forums/bind-data-to-radrichtextbox. Importing is the only appropriate option to insert text in the RadRichTextBox control. It is up to you from where to take the text. You can use the RadTextBox as a connection to transfer changes from the DataTable to the RadRichTextBox. Here is sample approach:
    DataTable dt = new DataTable();
      
    public Form1()
    {
        InitializeComponent();
      
        RadRichTextBinder binder = new RadRichTextBinder(this.radRichTextBox1);
        this.radTextBox1.DataBindings.Add("Text", binder, "Text", true, DataSourceUpdateMode.OnPropertyChanged);
        
        dt.Columns.Add("IdCol", typeof(int));
        dt.Columns.Add("TextCol", typeof(string));
      
        dt.Rows.Add(1, "");
          
        DataRow row = dt.Rows[0];
        DataView dv = row.Table.DefaultView;
      
        this.radTextBox1.TextBoxElement.DataBindings.Add("Text", dv, "TextCol");
    }
      
    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);
            }
        }
    }
      
    private void radButton1_Click(object sender, EventArgs e)
    {
        DataRow row = dt.Rows[0];
        row["TextCol"] = DateTime.Now ;
    }
      
    private void Form1_Load(object sender, EventArgs e)
    {
        DataRow row = dt.Rows[0];
        row["TextCol"] = DateTime.Now ;
    }

    Please find attached a sample video (drag and drop over the browser to play), demonstrating the behavior on my end. I would like to note that this is quite a sample implementation just to give you an idea. It may not cover all possible cases, so feel free to modify it on a way which suits your scenario best.

    Regards,
    Desislava
    Telerik

    Check out the new Telerik Platform - the only modular platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native apps. Register for the free online keynote and webinar to learn more about the Platform on Wednesday, February 12, 2014 at 11:00 a.m. ET (8:00 a.m. PT).

  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top