如何从文本框中突出显示DataGridView类型中的文本? [英] How to Highlight the Text in DataGridView Type from Text Box?

查看:64
本文介绍了如何从文本框中突出显示DataGridView类型中的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专家



我有一个文本框。我在其中输入一些文本。

现在我想在Datagridview中搜索该文本。

我也可以搜索它,我将突出显示该单元格。

但问题是我不想强调全细胞。我想要高光只搜索文本框文本。



如果我搜索'abc'并且如果我在DataGridview中找到它那么我将改变该单元格的颜色。但我想只强调'abc'。不是全细胞。



谢谢和提前

Sreeni

解决方案

你好Sreenivas,



这是用于突出显示Datagrid控件中搜索文本的代码段。这里我使用了WPF控件的Datagrid控件。您可以使用WPF Datagrid控件从WinForms更改Datagridview对象。请为此中使用的类添加一些命名空间。



但我想你会用这段代码解决你的问题。



  private   void  HighlighMatchedData(< span class =code-keyword> string  dataToFind)
{
try
{
< span class =code-keyword> var rows = ordinaryUsedFunctionsService.GetDataGridRows(UCPagingControl.DGControl);
foreach (DataGridRow dgr 行)
{
foreach (DataGridColumn dgColumn in UCPagingControl.DGControl.Columns)
{
FrameworkElement element = dgColumn .GetCellContent(DGR);
if (element!= null
{
if (!element.GetType()。Equals( typeof (ContentPresenter))&& element.GetType( ).Equals( typeof (TextBlock)))
{
// < span class =code-comment> object textBlock = commonUsedFunctionsService.FindVisualChild< TextBlock>(element);
// ((TextBox)textBox).Background = new SolidColorBrush(color);
TextBlock tb =(TextBlock)element;
ApplyColorHighlight(tb,dataToFind);
}
}
else
{
object textBlock = commonUsedFunctionsService.FindVisualChild< TextBlock>(dgColumn);
TextBlock tb =(TextBlock)元素;
ApplyColorHighlight(tb,dataToFind);
}
}
}
}
catch (例外情况)
{logService。 WriteExceptionLogToXML(GlobalMembers.InstanceGlobalMembers.exceptionXMLFilePath,ex); }
}


私人 void ApplyColorHighlight( TextBlock textBlock, string dataToFind)
{
try
{
string textBlockText = textBlock.Text;
int index = 0 ;
if (IsMatchCase)
{index = textBlockText.IndexOf(dataToFind,(IsMatchCase)?StringComparison.Ordinal:StringComparison.OrdinalIgnoreCase); }
else
{index = textBlockText.ToLower()。IndexOf(dataToFind.ToLower(), 0 ); }

// if(textBlockText.Contains(dataToFind))
if (index > -1)
{
// int index = textBlockText.IndexOf(dataToFind,(tb.IsCaseSensitive)?StringComparison.Ordinal:StringComparison.OrdinalIgnoreCase);
string subStringStart = textBlockText.Substring( 0 ,index);
textBlock.Inlines.Clear();
if (index < 0 // 如果文本中不存在highlightPhrase
{
textBlock.Inlines.Add(textBlockText); // 添加没有背景突出显示的文字到tb.Inlines
}
else
{
if (index > 0 // 如果在文本开始后发生highlightPhrase
textBlock.Inlines.Add(textBlockText.Substring( 0 ,index)); // 将highlightPhrase之前存在的文本(没有背景突出显示)添加到tb.Inlines
// 添加highlightPhrase,使用子字符串获取文本中显示的外壳,背景为tb。内联
textBlock.Inlines.Add( new 运行(textBlockText.Substring(index,dataToFind.Length))
{
// Background = tb.HighlightBrush
背景= new SolidColorBrush(System.Windows.Media.Colors.Yellow)
});
index + = dataToFind.Length; // 将索引移至匹配的highlightPhrase的结尾
if (index < textBlockText.Length) // 如果匹配的highlightPhrase的结尾发生在文本结尾之前
textBlock.Inlines.Add(textBlockText.Substring(index)); // 将highlightPhrase后面没有背景突出显示的文本添加到tb.Inlines
}
}
else
{
textBlock.Inlines.Clear();
textBlock.Inlines.Add( new 运行(textBlockText)
{
// Background = tb.HighlightBrush
Background = new SolidColorBrush(System.Windows。 Media.Colors.Transparent)
});

}
}
catch (例外情况)
{}
}


你好,

问一个类似问题此处

Dear Experts

I have one textbox. In which i am enter some text.
Now i want to search that text in the Datagridview.
I am also able to search that and i am going to highlight that cell.
But the problem is that i don`t want to highlight whole cell. I want to high light only search textbox text.

If i search 'abc' and if i found that in DataGridview then i am going to change the color of that cell. But i want to highlight only 'abc'. Not whole cell.

Thanks & Advance
Sreeni

解决方案

Hi Sreenivashan,

Here is the code snippet for highlighting the searched text in Datagrid control. Here i have used the Datagrid control from WPF control. you can change Datagridview object from WinForms with WPF Datagrid control. Kindly include some of the namespaces for the classes used in this.

But i guess you will get solution for your problem with this code.

private void HighlighMatchedData(string dataToFind)
        {
            try
            {
                var rows = commonlyUsedFunctionsService.GetDataGridRows(UCPagingControl.DGControl);
                foreach (DataGridRow dgr in rows)
                {
                    foreach (DataGridColumn dgColumn in UCPagingControl.DGControl.Columns)
                    {
                        FrameworkElement element = dgColumn.GetCellContent(dgr);
                        if (element != null)
                        {
                            if (!element.GetType().Equals(typeof(ContentPresenter)) && element.GetType().Equals(typeof(TextBlock)))
                            {
                                //object textBlock = commonlyUsedFunctionsService.FindVisualChild<TextBlock>(element);
                                //((TextBox)textBox).Background = new SolidColorBrush(color);
                                TextBlock tb = (TextBlock)element;
                                ApplyColorHighlight(tb, dataToFind);
                            }
                        }
                        else
                        {
                            object textBlock = commonlyUsedFunctionsService.FindVisualChild<TextBlock>(dgColumn);
                            TextBlock tb = (TextBlock)element;
                            ApplyColorHighlight(tb, dataToFind);
                        }
                    }
                }
            }
            catch (Exception ex)
            { logService.WriteExceptionLogToXML(GlobalMembers.InstanceGlobalMembers.exceptionXMLFilePath, ex); }
        }


        private void ApplyColorHighlight(TextBlock textBlock, string dataToFind)
        {
            try
            {
                string textBlockText = textBlock.Text;
                int index = 0;
                if (IsMatchCase)
                { index = textBlockText.IndexOf(dataToFind, (IsMatchCase) ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase); }
                else
                { index = textBlockText.ToLower().IndexOf(dataToFind.ToLower(), 0); }

                //if (textBlockText.Contains(dataToFind))
                if (index > -1)
                {
                    //int index = textBlockText.IndexOf(dataToFind, (tb.IsCaseSensitive) ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase);
                    string subStringStart = textBlockText.Substring(0, index);
                    textBlock.Inlines.Clear();
                    if (index < 0) //if highlightPhrase doesn't exist in text
                    {
                        textBlock.Inlines.Add(textBlockText); //add text, with no background highlighting, to tb.Inlines
                    }
                    else
                    {
                        if (index > 0) //if highlightPhrase occurs after start of text
                            textBlock.Inlines.Add(textBlockText.Substring(0, index)); //add the text that exists before highlightPhrase, with no background highlighting, to tb.Inlines
                        //add the highlightPhrase, using substring to get the casing as it appears in text, with a background, to tb.Inlines
                        textBlock.Inlines.Add(new Run(textBlockText.Substring(index, dataToFind.Length))
                        {
                            //Background = tb.HighlightBrush
                            Background = new SolidColorBrush(System.Windows.Media.Colors.Yellow)
                        });
                        index += dataToFind.Length; //move index to the end of the matched highlightPhrase
                        if (index < textBlockText.Length) //if the end of the matched highlightPhrase occurs before the end of text
                            textBlock.Inlines.Add(textBlockText.Substring(index)); //add the text that exists after highlightPhrase, with no background highlighting, to tb.Inlines
                    }
                }
                else
                {
                    textBlock.Inlines.Clear();
                    textBlock.Inlines.Add(new Run(textBlockText)
                    {
                        //Background = tb.HighlightBrush
                        Background = new SolidColorBrush(System.Windows.Media.Colors.Transparent)
                    });

                }
            }
            catch (Exception ex)
            { }
        }


Hello ,
A simlar question is asked here


这篇关于如何从文本框中突出显示DataGridView类型中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆