控制DataGridView的cell中具体字符的颜色

来源:互联网 发布:ge矩阵法 编辑:程序博客网 时间:2024/04/29 04:13

要实现此功能,首先我们就要知道DataGridView是怎么画单元格的,因此我们可以利用微软的这个事件来dataGridView1_CellPainting来实现我们要的效果:

具体代码如下:

//首先创建一个窗体:在Load事件中写入:int clength = 0;        private void Form2_Load(object sender, EventArgs e)        {            DataTable dt = new DataTable();            dt.Columns.Add("c1");            dt.Columns.Add("c2");            for (int j = 0; j < 10; j++)            {                dt.Rows.Add("你23ABI扩8873大", "bbbb");            }            this.dataGridView1.DataSource = dt;            for (int j = 0; j < 10; j++)            {                int height = TextRenderer.MeasureText(                    this.dataGridView1[0, j].Value.ToString(),                    this.dataGridView1.DefaultCellStyle.Font).Width;                this.dataGridView1.Rows[j].Height = height;            }            this.dataGridView1.CellPainting += new                 DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);         }
在接下来的代码中,将对中文,字母,数字通过不同的显示颜色来区分:
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)        {                        if (e.ColumnIndex == 0 && e.RowIndex > -1 && e.Value != null)            {                e.Paint(e.CellBounds, DataGridViewPaintParts.All                     & ~DataGridViewPaintParts.ContentForeground);                               clength = e.CellBounds.Location.X;                foreach (char c in e.Value.ToString())                {                    if (char.IsDigit(c))                    {                        int length = TextRenderer.MeasureText(c.ToString(), this.dataGridView1.DefaultCellStyle.Font).Width;                                              e.Graphics.DrawString(c.ToString(), this.dataGridView1.DefaultCellStyle.Font,                            new SolidBrush(Color.Red), clength, e.CellBounds.Location.Y+this.dataGridView1.Rows[e.RowIndex].Height/2-5);                        clength += Convert.ToInt32(length / 2);                                            }                    else if (char.IsUpper(c)||char.IsLower(c))                    {                        int length = TextRenderer.MeasureText(c.ToString(), this.dataGridView1.DefaultCellStyle.Font).Width;                        e.Graphics.DrawString(c.ToString(), this.dataGridView1.DefaultCellStyle.Font,                            new SolidBrush(Color.Gold), clength, e.CellBounds.Location.Y + this.dataGridView1.Rows[e.RowIndex].Height / 2-5);                        clength += length / 2;                    }                    else                    {                        int length = TextRenderer.MeasureText(c.ToString(), this.dataGridView1.DefaultCellStyle.Font).Width;                        e.Graphics.DrawString(c.ToString(), this.dataGridView1.DefaultCellStyle.Font,                            new SolidBrush(Color.Green), clength, e.CellBounds.Location.Y + this.dataGridView1.Rows[e.RowIndex].Height / 2-5);                        clength += length / 2 + 4;                                                                   }                                 }                e.Handled = true;            }        }
效果如下: