欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZB.QueueSys.Common
{
    public class DataGridViewHelper
    {
        //定义一个用于保存静态变量的实例
        private static DataGridViewHelper instance = null;
        //定义一个保证线程同步的标识
        private static readonly object locker = new object();
        //构造函数为私有,使外界不能创建该类的实例
        private DataGridViewHelper() { }
        public static DataGridViewHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null) instance = new DataGridViewHelper();
                    }
                }
                return instance;
            }
        }

        public void SelectAllDgv(DataGridView dgv, string cellName, bool isChecked)
        {
            if (dgv.Rows.Count == 0) return;

            dgv.ClearSelection();
            int count = dgv.Rows.Count;
            for (int i = 0; i < count; i++)
            {
                dgv.Rows[i].Cells[cellName].Value = isChecked;
            }
            dgv.RefreshEdit();
        }

        public void SetDgvRowColor(DataGridView dgv)
        {
            dgv.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(204, 224, 233);
        }

        public void SetDgvRowColor(DataGridView dgv, Color color)
        {
            dgv.AlternatingRowsDefaultCellStyle.BackColor = color;
            dgv.AutoGenerateColumns = false;
        }

        public void SetDgvStyle(DataGridView dgv, Color color)
        {
            dgv.AlternatingRowsDefaultCellStyle.BackColor = color;
            dgv.AutoGenerateColumns = false;
        }

        /// <summary>
        /// 重绘DataGridView
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="e"></param>
        public void PaintDgv(DataGridView dgv, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                SolidBrush b = new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor);
                e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture),
                    dgv.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 5, e.RowBounds.Location.Y + 4);
            }
            catch { return; }
        }

    }
}

  

posted on 2019-09-27 14:51  sunwugang  阅读(429)  评论(0编辑  收藏  举报