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

Select Text in RichTextBox inside RadGridView

3 Answers 311 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alan
Top achievements
Rank 1
Alan asked on 29 Sep 2013, 07:08 PM
How can I select text and highlight with a yellow background inside a radRichTextBox which is inside a radgridview from c# code behind.   Here is my XAML, but when I try to access the radRichTextBox from c# the name is not found?
<telerik:GridViewDataColumn Width="300*" Header="Paragraph" IsGroupable="False"  IsFilterable="True" Name="Paragraph" TextWrapping="Wrap" DataMemberBinding="{Binding Paragraph}">
 
                                   <telerik:GridViewDataColumn.CellTemplate>
                                       <DataTemplate>
                                           <Grid>
                                               <telerik:RadRichTextBox x:Name="radRichPar" FontSize="10" />
                                               <telerik:TxtDataProvider RichTextBox="{Binding ElementName=radRichPar}"  Text="{Binding Path=Paragraph, Mode=TwoWay}"/>
                                           </Grid>
                                       </DataTemplate>
                                   </telerik:GridViewDataColumn.CellTemplate>

from C# I want to access radRichPar to highlight some text programmatically..........it does not appear in intellisense......thanks

 

 

 

 

3 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 30 Sep 2013, 06:22 AM
Hi Alan,

You cannot access an element defined in DataTemplate with its name. What you can try is to work with ChildrenOfType<T>() extension method, find the exact RichTextBox that you want  and work with that instance. Check out this blog post for a reference. 

Regards,
Maya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
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
Alan
Top achievements
Rank 1
answered on 04 Oct 2013, 02:32 AM

Hi thank you for the response, I tried using the  ChildrenOfType<T>()  which is able to access the radRichTextbox in my radgridview, but it doesn't always work.  For example if I want to change the word "Parrots" to "this is a test"  the following code will not execute on the initial load of data, but will execute as soon as I move the scroll bar down to the next item on the grid?   The background color however will set properly on the load?   Is there another event I should be using, I have tried all the other "load" events with no success?

private void radGridView1_CellLoaded(object sender, CellEventArgs e)
       {
           if (e.Cell.Column.Name == "Paragraph")
           {
               var cells = radGridView1.ChildrenOfType<RadRichTextBox>().Where(c => c.Name == "radRichPar").ToList();
               //cells.ForEach(c => c.Background = Brushes.LightBlue);
               //cells.ForEach(c => c.Document.Selection.SelectAll());
               foreach (var y in cells)
               {
 
                   //y.Document.Selection.SelectAll();
 
 
                   y.Background = Brushes.LightBlue;
                   //y.Document.Selection.SelectAll();
 
                   DocumentTextSearch search = new DocumentTextSearch(y.Document);
 
                   foreach (var textRange in search.FindAll("Parrots"))
                   {
                       y.Document.Selection.AddSelectionStart(textRange.StartPosition); y.Document.Selection.AddSelectionEnd(textRange.EndPosition);
 
                       y.Insert("THIS IS  A TEST");
 
                   }
 
 
                   //RadGridViewCommands.c.Execute(null);
 
                   y.UpdateLayout();
               }
 
           }
 
0
Maya
Telerik team
answered on 04 Oct 2013, 07:12 AM
Hello Alan,

Can you try handling the events of the RichTextBox directly (Loaded for example) instead of CellLoaded ?  Do you get the same behavior ?

Regards,
Maya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
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
GridView
Asked by
Alan
Top achievements
Rank 1
Answers by
Maya
Telerik team
Alan
Top achievements
Rank 1
Share this question
or