C# 如何在DataGridView中突出显示搜索文本?

C# 如何在DataGridView中突出显示搜索文本?,c#,winforms,datagridview,highlighting,C#,Winforms,Datagridview,Highlighting,我想突出显示DataGridView中给定的搜索文本。我尝试了cellFormatting事件来查找搜索文本的边界并绘制FillRectangle,但无法准确地获取搜索文本的边界 在添加的图像中,我尝试突出显示文本o,但它也突出显示其他字符 谁能和我分享一下如何画一个完美的矩形来突出显示搜索到的文本 问候,, Amal Raj.您需要使用CellPainiting事件。请尝试以下代码: string keyValue = "Co"; //search text private void

我想突出显示DataGridView中给定的搜索文本。我尝试了cellFormatting事件来查找搜索文本的边界并绘制FillRectangle,但无法准确地获取搜索文本的边界

在添加的图像中,我尝试突出显示文本o,但它也突出显示其他字符

谁能和我分享一下如何画一个完美的矩形来突出显示搜索到的文本

问候,,
Amal Raj.

您需要使用CellPainiting事件。请尝试以下代码:

string keyValue = "Co"; //search text

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        if (e.Value == null) return;

        StringFormat sf = StringFormat.GenericTypographic;
        sf.FormatFlags = sf.FormatFlags | StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.DisplayFormatControl;
        e.PaintBackground(e.CellBounds, true);

        SolidBrush br = new SolidBrush(Color.White);
        if (((int)e.State & (int)DataGridViewElementStates.Selected) == 0)
            br.Color = Color.Black;

        string text = e.Value.ToString();
        SizeF textSize = e.Graphics.MeasureString(text, Font, e.CellBounds.Width, sf);

        int keyPos = text.IndexOf(keyValue, StringComparison.OrdinalIgnoreCase);
        if (keyPos >= 0)
        {
            SizeF textMetricSize = new SizeF(0, 0);
            if (keyPos >= 1)
            {
                string textMetric = text.Substring(0, keyPos);
                textMetricSize = e.Graphics.MeasureString(textMetric, Font, e.CellBounds.Width, sf);
            }

            SizeF keySize = e.Graphics.MeasureString(text.Substring(keyPos, keyValue.Length), Font, e.CellBounds.Width, sf);
            float left = e.CellBounds.Left + (keyPos <= 0 ? 0 : textMetricSize.Width) + 2;
            RectangleF keyRect = new RectangleF(left, e.CellBounds.Top + 1, keySize.Width, e.CellBounds.Height - 2);

            var fillBrush = new SolidBrush(Color.Yellow);
            e.Graphics.FillRectangle(fillBrush, keyRect);
            fillBrush.Dispose();
        }
        e.Graphics.DrawString(text, Font, br, new PointF(e.CellBounds.Left + 2, e.CellBounds.Top + (e.CellBounds.Height - textSize.Height) / 2), sf);
        e.Handled = true;

        br.Dispose();
    }

您需要使用CellPainiting事件。请尝试以下代码:

string keyValue = "Co"; //search text

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        if (e.Value == null) return;

        StringFormat sf = StringFormat.GenericTypographic;
        sf.FormatFlags = sf.FormatFlags | StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.DisplayFormatControl;
        e.PaintBackground(e.CellBounds, true);

        SolidBrush br = new SolidBrush(Color.White);
        if (((int)e.State & (int)DataGridViewElementStates.Selected) == 0)
            br.Color = Color.Black;

        string text = e.Value.ToString();
        SizeF textSize = e.Graphics.MeasureString(text, Font, e.CellBounds.Width, sf);

        int keyPos = text.IndexOf(keyValue, StringComparison.OrdinalIgnoreCase);
        if (keyPos >= 0)
        {
            SizeF textMetricSize = new SizeF(0, 0);
            if (keyPos >= 1)
            {
                string textMetric = text.Substring(0, keyPos);
                textMetricSize = e.Graphics.MeasureString(textMetric, Font, e.CellBounds.Width, sf);
            }

            SizeF keySize = e.Graphics.MeasureString(text.Substring(keyPos, keyValue.Length), Font, e.CellBounds.Width, sf);
            float left = e.CellBounds.Left + (keyPos <= 0 ? 0 : textMetricSize.Width) + 2;
            RectangleF keyRect = new RectangleF(left, e.CellBounds.Top + 1, keySize.Width, e.CellBounds.Height - 2);

            var fillBrush = new SolidBrush(Color.Yellow);
            e.Graphics.FillRectangle(fillBrush, keyRect);
            fillBrush.Dispose();
        }
        e.Graphics.DrawString(text, Font, br, new PointF(e.CellBounds.Left + 2, e.CellBounds.Top + (e.CellBounds.Height - textSize.Height) / 2), sf);
        e.Handled = true;

        br.Dispose();
    }

将您迄今为止尝试过的代码发布出来。请参考我所要求的WinForms技术。当您在网格方法中绑定数据时,必须执行某些条件。您可以从中找到一些东西。这确实很难做到正确。也许最好的选择是把所有的文本片段都用字符串表示出来。把你迄今为止尝试过的代码发布出来。请参考我所要求的WinForms技术。当您在网格方法中绑定数据时,必须执行某些条件。您可以从中找到一些东西。这确实很难做到正确。也许最好的选择是把所有的文本都用字符串表示出来。这个解决方案很好用。但是当我们设置WrapMode时,上面的格式将禁用WrapMode。在包装模式下,高亮显示不起作用。但是,对我来说,单元格的字体/布局/对齐方式与其他单元格完全不同,因此我必须对此进行更多研究。使用包装词的解决方案解决方案很好。但是当我们设置WrapMode时,上面的格式将禁用WrapMode。而突出显示在包装模式下不起作用。但是,对我来说,单元格的字体/布局/对齐方式与其他单元格完全不同,因此我必须进一步研究这个问题。使用包装词的解决方案