Winform DataGridView 绘制虚线行

在Winform的开发中,会遇到根据内容设置DataGridView的行的线为虚线,DataGridView自带不具备虚线功能,你可以重写,也可以其他方法,在这里我使用DrawBorder的方法。
一、关闭DataGridView自带的CellBorderStyle,即设置DataGridView的CellBorderStyle为none。

二、在行的绘制事件添加以下代码

private void dgvContent_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //条件判断
            if (e.RowIndex > 0 && dgvContent[0, e.RowIndex].Value != null)
            {
                //获取行边界
                Rectangle cellRect = e.RowBounds;
                //绘制边框
                ControlPaint.DrawBorder(e.Graphics,
                                        cellRect,
                                        Color.LightGray,
                                        0,
                                        ButtonBorderStyle.Dashed,
                                        Color.LightGray,
                                        1,
                                        ButtonBorderStyle.Dashed,
                                        Color.LightGray,
                                        0,
                                        ButtonBorderStyle.Dashed,
                                        Color.LightGray,
                                        0,
                                        ButtonBorderStyle.Dashed);
            }
        }
完成,手工

 

posted @ 2017-11-21 17:48  张三的歌丶  阅读(382)  评论(0编辑  收藏  举报