如何更改DataGridView中某些单元格的边框颜色? [英] How to change the border color of some cells in DataGridView?

查看:1001
本文介绍了如何更改DataGridView中某些单元格的边框颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编程改变CellFormatting事件中某些单元格的边框颜色。可以更改单个单元格的单板颜色吗?

I need to programming change the border color of some cells in the CellFormatting event. Can the board color of an individual cell be changed?

推荐答案

您可以绘制一个矩形。在这个例子中,我在选定的单元格上放了一个红色的编码器。

You can draw a rectangle. In this example I put a red boder on the selected cells.

private void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
    {
        if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected == true)
        {
            e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border);
            using (Pen p = new Pen(Color.Red, 1))
            {
                Rectangle rect = e.CellBounds;
                rect.Width -= 2;
                rect.Height -= 2;
                e.Graphics.DrawRectangle(p, rect);
            }
            e.Handled = true;
        }
    }
}

这篇关于如何更改DataGridView中某些单元格的边框颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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