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

Highlight/Style a Cell programmatically?

3 Answers 432 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Bob
Top achievements
Rank 3
Iron
Iron
Veteran
Bob asked on 06 Jul 2011, 09:49 PM
Hello,

I am using WPF RadGridView in a common user control, and this control supports many different types of data.  The grid is therefore built dynamically in code, and there is no XAML except to define the grid itself.  The data is bound using the ItemsSource property to a QueryableCollectionView.  Our client has requested that we provide a search on the visible grid elements (i.e. current page only), and they do NOT want to filter based on the search, only highlight the cells where the text was found.

I have implemented the code necessary to perform the search.  However, at the point where I find a match, I need to highlight the cell containing the match.  I know the row and column at this point, but I am unclear how to go about changing the cell style with only this information.  Do you have any suggestions?

Here is the event code to perform the search after the search text box (txtSearch) is filled:
 
private void SearchTextBox_Search(object sender, RoutedEventArgs e)
{
    int foundCellCount = 0;
    foreach (object o in ((QueryableCollectionView)theGrid.ItemsSource))
    {
        if (o != null)
        {
            Type rowType = o.GetType();
            PropertyInfo[] props = rowType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
 
            foreach (PropertyInfo pi in props)
            {
                if (IsVisibleColumn(pi.Name))
                {
                    object oCellVal = pi.GetValue(o, null);
                    if ((oCellVal != null) &&
                        ((oCellVal.GetType() == typeof(int)) ||
                         (oCellVal.GetType() == typeof(string)) ||
                         (oCellVal.GetType() == typeof(long))))
                    {
                        string cellVal = oCellVal.ToString();
                        if (cellVal.Contains(txtSearch.Text))
                        {
                            // Match found
                            foundCellCount++;
 
                            // NEED TO HIGHLIGHT THE CONTAINING CELL HERE!!!
                        }
                    }
                }
            }
        }
    }
 
    if (foundCellCount > 0)
    {
        MessageBox.Show(string.Format("Match found in {0} cells", foundCellCount));
    }
    else
    {
        MessageBox.Show("Not Found!");
    }
}

3 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 07 Jul 2011, 06:43 AM
Hi Bob,

Could you please take a look at this blog post. It demonstrates how you can filter the grid and highlight the matching texts but you can easily turn off the filtering and only use highlighting. 

Greetings,
Milan
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Bob
Top achievements
Rank 3
Iron
Iron
Veteran
answered on 07 Jul 2011, 03:20 PM
Thank you for that example.  I had already looked at that solution, and the problem is that I allow other developers using the grid user control to setup their own columns and cell templates.  I did not want to interfere with the column definitions to provide highlighting.  I was really hoping it would be simple to just change the style of a cell location within the grid code behind since I already know the position.  If the only way to change the style is with a bound template definition, I will have to rethink the entire design.
0
Milan
Telerik team
answered on 08 Jul 2011, 07:08 AM
Hi Bob,

I am afraid that there is not way to have highlighting without mending the template. You need to have custom controls inside the cells that are capable of highlighting text.

Greetings,
Milan
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
GridView
Asked by
Bob
Top achievements
Rank 3
Iron
Iron
Veteran
Answers by
Milan
Telerik team
Bob
Top achievements
Rank 3
Iron
Iron
Veteran
Share this question
or