当前位置:首页 > Windows程序 > 正文

DataGridView单元格美化

2021-05-24 Windows程序

标签:

技术分享

 

 

 

#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);

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70061.html