zoukankan      html  css  js  c++  java
  • DataGridView单元格美化

     
     
     
    #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   
  • 相关阅读:
    为什么单个TCP连接很难占满带宽
    上传NUnit的单元测试结果和OpenCover的单元测试覆盖率到SonarQube服务中
    使用Visual Studio Code Coverage和nunit上传单元测试覆盖率和单元测试结果到SonarQube上
    java安装1.8的经验和Error: Registry key 'SoftwareJavaSoftJava Runtime Environment'CurrentVers问题处理
    NSubstitute.Analyzers检测NSubstitute用法冲突
    在TeamCity中执行gtest单元测试
    iOS OpenGL ES入门
    iOS 基础知识
    【内推】字节跳动-头条小说&番茄小说
    iOS开发小记(十四)
  • 原文地址:https://www.cnblogs.com/xxaxx/p/4630864.html
Copyright © 2011-2022 走看看