Solved

Hightlighting searched text

Posted on 2010-09-24
3
261 Views
Last Modified: 2013-11-12
Experts,

I am using a sample third party grid from SyncFusion(they wont support unless i am ready to buy) and I am using one of their Grid control to impplment search functionality.
Following code gives me tells me whether the cell contains the searched value:
gridControl.Model[e.Cell.RowIndex, e.Cell.ColumnIndex].Text.Contains(SearchValue)
All i am trying to do is that instead of highlighting the whole cell using the code :
gridControl.Model[e.Cell.RowIndex, e.Cell.ColumnIndex].Background = Brushes.Yellow;
I  want to only highlight the matching text. Can you please help me with this urgent issue ?
0
Comment
Question by:dotnetpro
  • 2
3 Comments
 
LVL 4

Expert Comment

by:grishav
ID: 33769748
Hi,
I don't think that you have obvious way to do this - no matter which grid you use.
You probably need to make a DataTemplate for you cell content and specify that it consists
from 3 text blocks - one for the substring of the text before selection, one for selected text, and one for the rest of string.
You should get rid of margins&padding to get the smooth picture of the whole text.
And you still must maintain string property for the whole text in your data class.

In "normal" cell you can just set the text in the first block to be the whole string and make the rest an empty string.
While searching, when you see that the cell text matches the pattern,
you need to split the string into 3 parts, assign values to the properties which are bound to the content of 3 text blocks and change background only for the second one.
0
 
LVL 1

Author Comment

by:dotnetpro
ID: 33772040
Can you please give me a quick example as well ?
0
 
LVL 4

Accepted Solution

by:
grishav earned 500 total points
ID: 33776738
I never worked with SyncFusion grid, so you'll have to find out how to attach the DataTemplate to its cell. Here you can see how to do this with ListBox:

Xaml ("MainWindow.xaml") :

<Window x:Class="EESample.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:local="clr-namespace:EESample"



        Title="MainWindow" Height="350" Width="525">

    

    <StackPanel Orientation="Vertical">

        <ListBox x:Name="myList" Height="200">

            <ListBox.ItemTemplate>

                <DataTemplate DataType="local:MyCellContent">

                    <StackPanel Orientation="Horizontal" Height="20">

                        <TextBlock Text="{Binding Path=Prefix}"/>

                        <TextBlock Text="{Binding Path=SelectedText}" Background="Yellow"/>

                        <TextBlock Text="{Binding Path=Suffix}"/>

                    </StackPanel>

                </DataTemplate>

            </ListBox.ItemTemplate>

        </ListBox>

        <TextBlock Margin="0,10,0,0" Text="Enter text to search:"/>

        <TextBox Name="searchText"/>

        <Button Content="Highlight!" Click="Button_Click"/>

    </StackPanel>

</Window>



C#:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

using System.Collections;

using System.ComponentModel;



namespace EESample

{

	/// <summary>

	/// Interaction logic for MainWindow.xaml

	/// </summary>

	public partial class MainWindow : Window

	{

		public ArrayList Data;

		public MainWindow()

		{

			InitializeComponent();

			Data = new ArrayList();

			Data.Add(new MyCellContent("one two three"));

			Data.Add(new MyCellContent("four five six"));

			Data.Add(new MyCellContent("seven eight nine"));

			myList.ItemsSource = Data;

			

		}

		private void Button_Click(object sender, RoutedEventArgs e)

		{

			string pattern = searchText.Text;

			int index;

			foreach (MyCellContent cc in Data) {

				index = cc.ContentText.IndexOf(pattern);

				if (index >= 0) {

					cc.Prefix = cc.ContentText.Substring(0, index);

					cc.SelectedText = cc.ContentText.Substring(index, pattern.Length);

					cc.Suffix = cc.ContentText.Substring(index + pattern.Length);

				}

				else {

					cc.Prefix = cc.ContentText;

					cc.Suffix = cc.SelectedText = string.Empty;

				}

			}

		}

	}

	public class MyCellContent : DependencyObject, INotifyPropertyChanged

	{

		string _content_text;

		public string ContentText

		{

			get

			{

				return _content_text;

			}

			set

			{

				_content_text = Prefix = value;

				Suffix = SelectedText = string.Empty;

			}

		}

		string _prefix;

		public string Prefix

		{

			get { return _prefix; }

			set { _prefix = value; OnPropertyChanged("Prefix"); }

		}

		string _suffix;

		public string Suffix

		{

			get { return _suffix; } 

			set { _suffix = value; OnPropertyChanged("Suffix"); }

		}

		string _sel;

		public string SelectedText

		{

			get { return _sel; }

			set { _sel = value; OnPropertyChanged("SelectedText"); }

		}

		public MyCellContent(string text)

		{

			ContentText = text;

		}



		#region INotifyPropertyChanged Members



		public event PropertyChangedEventHandler PropertyChanged;

		public void OnPropertyChanged(string name)

		{

			if (PropertyChanged != null)

				PropertyChanged(this, new PropertyChangedEventArgs(name));

		}

		#endregion

	}



}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Get a List of Executing Stored Procedures on Sql Server 2008 R2 5 47
Best book to learn C++ 4 70
Need more details 5 122
How to LOAD up a .NET Framework website with data? 2 39
This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now