vb.net颜色在单元格中的第一个字符 [英] vb.net color first char in cell

查看:131
本文介绍了vb.net颜色在单元格中的第一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB .NET我有3个字符,根据一些计算添加到DataGridView单元格。

In VB .NET I have 3 characters which are added to a DataGridView cell depending on some calculations.

它们是等级变化箭头和工作正常,但我想向上箭头为绿色,向下箭头为红色。

They are rank change arrows and work fine, but I want the up arrow to be green and the down arrow to be red.

Dim strup As String = "▲"
Dim strdown As String = "▼"
Dim strsame As String = "▬"

在单元格中,负三的变化将看起来像▼3和加3将看起来像▲3,其中文本和符号是不同的颜色。

So in the cell a change of negative three will look like ▼3 and plus 3 will look like ▲3 where the text and symbol are different colors.

如何更改DataGridView单元格中第一个字符的颜色?

How can I change the color of the first character in DataGridView cell?

推荐答案

没有简单的方法来做到这一点,如果你有任何东西,不仅仅是在单元格中的问题的字符(你需要做一些形式的自定义绘画)。

There is no easy way to do this if you have anything more than just the character in question in the cell (you would need to do some form of custom painting).

如果你只有那些字符,那么这很容易与CellFormatting事件:

If you only have those characters then this is very easy with the CellFormatting event:

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    e.CellStyle.Font = new Font("Arial Unicode MS", 12);
    if (dataGridView1.Columns[e.ColumnIndex].Name == "CorrectColumnName")
    {
        if (e.Value == "▲")
            e.CellStyle.ForeColor = Color.Green;
        else if (e.Value == "▼")
            e.CellStyle.ForeColor = Color.Red;
        else
            e.CellStyle.ForeColor = Color.Black;
    }
}






如果你想在同一个单元格中使用不同的颜色,那么需要类似下面的代码(这将处理CellPainting事件):


If you do want different colors within the same cell then something like the following code is required (this handles the CellPainting event):

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.ColumnIndex == -1 || e.RowIndex == -1)
        return;

    if (dataGridView1.Columns[e.ColumnIndex].Name == "CorrectColumnName")
    {
        e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground);

        if (e.FormattedValue.ToString().StartsWith("▲", StringComparison.InvariantCulture))
        {
            RenderCellText(Color.Green, e);
        }
        else if (e.FormattedValue == "▼")
        {
            RenderCellText(Color.Red, e);
        }
        else
            RenderCellText(SystemColors.WindowText, e);

        e.Handled = true;
    }
}

private void RenderCellText(Color color, DataGridViewCellPaintingEventArgs e)
{
    string text = e.FormattedValue.ToString();
    string beginning = text.Substring(0, 1);
    string end = text.Substring(1);
    Point topLeft = new Point(e.CellBounds.X, e.CellBounds.Y + (e.CellBounds.Height / 4));

    TextRenderer.DrawText(e.Graphics, beginning, this.dataGridView1.Font, topLeft, color);
    Size s = TextRenderer.MeasureText(beginning, this.dataGridView1.Font);

    Point p = new Point(topLeft.X + s.Width, topLeft.Y);
    TextRenderer.DrawText(e.Graphics, end, this.dataGridView1.Font, p, SystemColors.WindowText);
}

这篇关于vb.net颜色在单元格中的第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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