DataGridView CellPainting:考虑单元格的内容方向,突出显示单元格中的单词

时间:2014-03-11 10:06:51

标签: c# winforms datagridview highlighting paintevent

情况就是这样:我想通过循环DataGridView来搜索整个Row中的单词,然后为每个匹配我突出显示该单词。我面临的主要缺点是列具有不同的System.Windows.Forms.DataGridViewContentAlignment s。所以在paiting(突出显示)单元格中的数学单词时,我应该处理单元格的ContentAlignment

到目前为止,我已经编写了以下代码来查找匹配项

private int FindAllMatches()
{
    int itemsFound = 0;
    for (int r = 0; r < dgvMain.Rows.Count; r++)
    {
        DataGridViewRow row = dgvMain.Rows[r];
        for (int c = 0; c < Columns.Count; c++)
        {
            string cellValue = (dgvMain.Rows[r].Cells[c].Value ?? "").ToString();
            if (cellValue.Contains(SearchValue.ToString()))
            {
                HighlightRow(row); // highlights whole row, weak solution
                itemsFound++;
                break;
            }
        }
    }
    return itemsFound;
}

但正如您所见,我目前设法突出显示至少有一个匹配的行。我需要的是一个代码,只突出显示与我的特定单词匹配的单元格部分( SearchValue )。

我知道我应该使用CellPainting事件,但我不知道如何在考虑Cell的{​​{1}} Cell内绘制这个词p>

1 个答案:

答案 0 :(得分:1)

我修改了我对此question的回答,以考虑方向和内容对齐。它适用于默认对齐,但您必须改进其他对齐的代码。

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex != -1 && e.Value != null && e.Value.ToString().Length > 5 && e.ColumnIndex == 2)
    {
        if (!e.Handled)
        {
            e.Handled = true;
            e.PaintBackground(e.CellBounds, dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected);
        }
        if ((e.PaintParts & DataGridViewPaintParts.ContentForeground) != DataGridViewPaintParts.None)
        {
            bool rightToLeft = this.RightToLeft == RightToLeft.Yes ? true:false;
            string text = e.Value.ToString();
            string textPart1 = text.Substring(0, text.Length - 5);
            string textPart2 = text.Substring(text.Length - 5, 5);
            Size fullsize = TextRenderer.MeasureText(text, e.CellStyle.Font);
            Size size1 = TextRenderer.MeasureText(textPart1, e.CellStyle.Font);
            Size size2 = TextRenderer.MeasureText(textPart2, e.CellStyle.Font);
            Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
            TextFormatFlags flags = GetFlagsForCellStyleAlignment(rightToLeft, e.CellStyle.Alignment);
            TextRenderer.DrawText(e.Graphics, text, e.CellStyle.Font, rect1, Color.Crimson, flags);
            using (Brush cellForeBrush = new SolidBrush(e.CellStyle.ForeColor))
            {
                TextRenderer.DrawText(e.Graphics, textPart1, e.CellStyle.Font, rect1, e.CellStyle.ForeColor, flags);
            }
        }
    }
}

TextFormatFlags GetFlagsForCellStyleAlignment(bool rigthToLeft, DataGridViewContentAlignment alignment)
{
    TextFormatFlags flags = TextFormatFlags.Default;
    switch (alignment)
    {
        case DataGridViewContentAlignment.TopLeft:
            {
                flags = TextFormatFlags.Default;
                if (rigthToLeft)
                {
                    flags |= TextFormatFlags.Right;
                }
                break;
            }
        case DataGridViewContentAlignment.MiddleLeft:
            {
                flags = TextFormatFlags.VerticalCenter;
                if (rigthToLeft)
                {
                    flags |= TextFormatFlags.Right;
                }
                break;
            }
    }
    if (rigthToLeft)
        flags |= TextFormatFlags.RightToLeft;
    return flags;
}