You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在DataGridView指定列中高亮显示搜索文本?

仅在DataGridView指定列高亮搜索文本的解决方案

当然可以实现只在指定列高亮搜索文本!只需要在原有的CellPainting事件处理逻辑中,添加一个列判断条件,限定仅对目标列执行高亮渲染即可。

核心修改思路

在单元格绘制前,先检查当前单元格是否属于你指定的列(可以通过列索引或列名来指定),只有符合条件的列才执行高亮逻辑,其他列保持默认渲染。

修改后的完整代码示例

string keyValue = "Co"; // 搜索文本

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    // 1. 跳过列头、空值,以及非目标列的单元格
    if (e.RowIndex < 0 || e.Value == null || 
        // 这里指定要高亮的列:可以用列名获取索引,或者直接写列索引(如e.ColumnIndex == 1)
        e.ColumnIndex != dataGridView1.Columns["ProductName"].Index)
    {
        // 非目标列直接使用默认绘制
        e.Paint(e.CellBounds, DataGridViewPaintParts.All);
        e.Handled = true;
        return;
    }

    // 2. 目标列执行高亮逻辑
    string cellText = e.Value.ToString();
    if (!cellText.Contains(keyValue))
    {
        // 单元格文本不含搜索关键词,默认绘制
        e.Paint(e.CellBounds, DataGridViewPaintParts.All);
        e.Handled = true;
        return;
    }

    // 3. 绘制单元格背景和边框
    e.Paint(e.CellBounds, DataGridViewPaintParts.Background | DataGridViewPaintParts.Border);

    // 4. 拆分文本,分别绘制普通部分和高亮部分
    StringFormat sf = StringFormat.GenericTypographic;
    sf.FormatFlags |= StringFormatFlags.NoClip;
    sf.Alignment = StringAlignment.Near;

    int keyStartIndex = cellText.IndexOf(keyValue, StringComparison.OrdinalIgnoreCase);
    string beforeKey = cellText.Substring(0, keyStartIndex);
    string keyPart = cellText.Substring(keyStartIndex, keyValue.Length);
    string afterKey = cellText.Substring(keyStartIndex + keyValue.Length);

    // 绘制关键词前的文本(默认样式)
    using (Brush normalBrush = new SolidBrush(e.CellStyle.ForeColor))
    {
        e.Graphics.DrawString(beforeKey, e.CellStyle.Font, normalBrush, 
            e.CellBounds.Left + 2, e.CellBounds.Top + 2, sf);
    }

    // 绘制高亮的关键词(自定义背景色)
    SizeF keySize = e.Graphics.MeasureString(keyPart, e.CellStyle.Font);
    RectangleF keyRect = new RectangleF(
        e.CellBounds.Left + 2 + e.Graphics.MeasureString(beforeKey, e.CellStyle.Font).Width,
        e.CellBounds.Top + 2,
        keySize.Width,
        keySize.Height);

    using (Brush highlightBrush = new SolidBrush(Color.Yellow))
    {
        e.Graphics.FillRectangle(highlightBrush, keyRect);
    }
    using (Brush keyBrush = new SolidBrush(Color.Black))
    {
        e.Graphics.DrawString(keyPart, e.CellStyle.Font, keyBrush, keyRect, sf);
    }

    // 绘制关键词后的文本(默认样式)
    using (Brush normalBrush = new SolidBrush(e.CellStyle.ForeColor))
    {
        e.Graphics.DrawString(afterKey, e.CellStyle.Font, normalBrush,
            e.CellBounds.Left + 2 + e.Graphics.MeasureString(beforeKey + keyPart, e.CellStyle.Font).Width,
            e.CellBounds.Top + 2, sf);
    }

    e.Handled = true;
}

关键说明

  • 指定目标列:代码中e.ColumnIndex != dataGridView1.Columns["ProductName"].Index这行是核心判断,你可以把"ProductName"改成你需要高亮的列名,也可以直接用列索引(比如e.ColumnIndex == 2,注意索引从0开始)。
  • 大小写不敏感搜索:如果需要忽略大小写,IndexOf方法用StringComparison.OrdinalIgnoreCase参数即可;如果需要严格匹配大小写,去掉这个参数就行。
  • 自定义高亮样式:你可以修改Color.Yellow为其他颜色,或者调整字体样式来改变高亮效果。

内容的提问来源于stack exchange,提问作者DaniKR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:01:26