#region 重绘Column、Row
 
        int _RowHeadWidth = 41;
        ///   
        /// 重绘Column、Row   
        ///   
        ///   
        /// 
        private void gdvPersonInfo_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            //如果是Column   
            if (e.RowIndex == -1)
            {
                drawColumnAndRow(e);
                e.Handled = true;
                //如果是Rowheader   
            }
            else if (e.ColumnIndex < 0 && e.RowIndex >= 0)
            {
                drawColumnAndRow(e);
                _RowHeadWidth = e.CellBounds.Width;
                e.Handled = true;
            }
        }
 
        ///   
        /// Column和RowHeader绘制   
        ///   
        ///   
        void drawColumnAndRow(DataGridViewCellPaintingEventArgs e)
        {
            // 绘制背景色   
            using (LinearGradientBrush backbrush =
                new LinearGradientBrush(e.CellBounds,
                    ProfessionalColors.MenuItemPressedGradientBegin,
                    ProfessionalColors.MenuItemPressedGradientMiddle
                    , LinearGradientMode.Vertical))
            {
 
                Rectangle border = e.CellBounds;
                border.Width -= 1;
                //填充绘制效果   
                e.Graphics.FillRectangle(backbrush, border);
                //绘制Column、Row的Text信息   
                e.PaintContent(e.CellBounds);  // 参数的意义?   
                //绘制边框   
                ControlPaint.DrawBorder3D(e.Graphics, e.CellBounds, Border3DStyle.Etched);
 
            }
        }
 
        #endregion
 
 
        #region 重绘选中状态
 
        #region Row重绘前处理
 
        ///   
        /// Row重绘前处理,绘制行样式   
        ///   
        ///
        /// 
        private void gdvPersonInfo_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
 
            //是否是选中状态   
            if ((e.State & DataGridViewElementStates.Selected) ==
                        DataGridViewElementStates.Selected)
            {
                // 计算选中区域Size   
                int width = this.gdvPersonInfo.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + _RowHeadWidth;
 
                Rectangle rowBounds = new Rectangle(
                  0, e.RowBounds.Top, width,
                    e.RowBounds.Height);
 
                // 绘制选中背景色   
                using (LinearGradientBrush backbrush =
                    new LinearGradientBrush(rowBounds,
                        Color.SteelBlue,
                        e.InheritedRowStyle.ForeColor, 90.0f))
                {
                    e.Graphics.FillRectangle(backbrush, rowBounds);
                    e.PaintCellsContent(rowBounds);
                    e.Handled = true;       //告诉系统,已经自己重绘过了,该次绘制任务到此结束   
                }
 
            }
        }
 
        #endregion
 
        #region Row重绘后处理
 
        ///   
        /// Row重绘后处理,目前显示效果不大   
        ///   
        ///   
        private void gdvPersonInfo_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            int width = this.gdvPersonInfo.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + _RowHeadWidth;
            Rectangle rowBounds = new Rectangle(
                   0, e.RowBounds.Top, width, e.RowBounds.Height);
 
            if (this.gdvPersonInfo.CurrentCellAddress.Y == e.RowIndex)
            {
                //设置选中边框,显示为虚线的聚焦框   
                e.DrawFocus(rowBounds, true);
            }
        }
 
        #endregion
 
        #region Row刷新
        ///   
        /// 宽度改变后处理,暂时没出现效果   
        ///   
        private void gdvPersonInfo_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
        {
            if (this.gdvPersonInfo.CurrentRow != null)
                this.gdvPersonInfo.InvalidateRow(this.gdvPersonInfo.CurrentRow.Index);
        }
 
        ///   
        /// 用户或代码滚动工作区时发生,暂没看见效果   
        ///   
        protected override void OnScroll(ScrollEventArgs e)
        {
            base.OnScroll(e);
            if (this.gdvPersonInfo.CurrentRow != null)
                this.gdvPersonInfo.InvalidateRow(this.gdvPersonInfo.CurrentRow.Index);
        }
 
        #endregion
 
        #endregion   

DataGridView单元格美化的更多相关文章

  1. winform中dataGridView单元格根据值设置新值,彻底解决绑定后数据类型转换的困难

    // winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现.有时候需要将数字变换为不同的文字描述,就会出现int32到string类型转换的异常,借助CellFo ...

  2. DataGridView单元格合并

    本文章转载:http://www.cnblogs.com/xiaofengfeng/p/3382094.html 图: 代码就是如此简单 文件下载:DataGridView单元格合并源码 也可以参考: ...

  3. DataGridView单元格显示GIF图片

    本文转载:http://home.cnblogs.com/group/topic/40730.html DataGridView单元格显示GIF图片 gifanimationindatagrid.ra ...

  4. Winfrom设置DataGridView单元格获得焦点(DataGridView - CurrentCell)

    设置DataGridView单元格获得焦点 this.dgv_prescription.BeginEdit(true);

  5. DataGridView 单元格自动填充

    在DataGridView单元格中,当输入指定字符时,自动完成填充. 通过 TextBox实现 AutoCompleteMode AutoCompleteMode.Suggest: AutoCompl ...

  6. Winform Datagridview 单元格html格式化支持富文本

    Winform Datagridview 单元格html格式化支持富文本 示例: 源码:https://github.com/OceanAirdrop/DataGridViewHTMLCell 参考: ...

  7. 设置DataGridView单元格的文本对齐方式

    实现效果: 知识运用: DataGridViewCellStyle类的Alignment属性     //获取或设置DataGridView单元格内的单元格内容的位置 public DataGridV ...

  8. 【Winform-自定义控件】DataGridView 单元格合并和二维表头

    DataGridView单元格合并和二维表头应用: //DataGridView绑定数据 DataTable dt = new DataTable(); dt.Columns.Add("); ...

  9. WinForm笔记1:TextBox编辑时和DataGridView 单元格编辑时 的事件及其顺序

    TextBox 编辑框 When you change the focus by using the mouse or by calling the Focus method, focus event ...

随机推荐

  1. C++产生随机数四则运算

    产生两位随机整数,随机四则运算符,生成30道运算题. 一.编程思路 看到要求,首先想到的是怎么运用随机数,因为自己对随机数的不熟练所以还要在查很多东西.在一个for循环内先产生两个30以内的随机数,在 ...

  2. Session 知识点再整理(二) 自定义 Session 存储机制

    对于访问量大的网站,用默认的 Session 存储方式(以文件存储)不适合,因为文件的 I/O 开销会非常大,另外 Session 机制本身使 Session 不能跨机访问,在 Web 集群中无法达到 ...

  3. asp.net identity 2.2.0 中角色启用和基本使用(六)

    创建用户管理相关视图 第一步:添加视图   打开UsersAdminController.cs   将鼠标移动到public ActionResult Index()上  右键>添加视图   系 ...

  4. 优秀API设计的十大原则

    优秀API设计的十大原则 2015-09-23    分类:编程开发.设计模式.首页精华暂无人评论 分享到:更多4 二十万年薪PHP工程师培养计划 成为被疯抢的Android牛人 风中叶讲Java重难 ...

  5. NEC学习 ---- 模块 - 上图下文图文列表

    上图下文图文列表的效果如下图: 可以看到三个红色框中的三中"上图下文的图文列表"; 这里的代码其实没什么问题, 对于这种布局, 其实可以参考我上一篇介绍: NEC学习 ---- 模 ...

  6. hibernate学习(5)——对象状态与一级缓存

    1.对象状态 1.1   状态介绍 hibernate 规定三种状态:瞬时态.持久态.脱管态 瞬时态:transient,session没有缓存对象,数据库也没有对应记录.没有与hibernate关联 ...

  7. KVC和KVO大优点

    都是动态的,运行时检查,给予了极大的方便

  8. Maven-006-手动部署第三方构件至 nexus 私服

    某些 Java 构件因许可证因素,无法公开的部署到公共仓库中:或者,一些小型的开源项目(例如 SourceForge.GitHub 中的一些项目),没有将构件分发到中央仓库中,也没有维护自己的仓库,因 ...

  9. iOS 1-2年经验面试参考题

    Model层: 数据持久化存储方案有哪些? 沙盒的目录结构是怎样的?各自一般用于什么场合? SQL语句问题:inner join.left join.right join的区别是什么? SQLite的 ...

  10. JQuery对象操作支持链式法则源码分析

    JQuery链式法则 何为链式法则?先给出非链式写法的例子 //非链式写法 $("div").css("width", 45px); $("div&q ...