C# Winform重写DataGridView 有兴趣可以留言交流

//[ToolboxBitmap(typeof(DataGrid))]
public partial class UCDataGridView : DataGridView
{
//鼠标移动进来改变颜色
private bool _CellColorOnchange = false;
private bool _PaintCell = true;
private bool _canRefresh = true;
private List _columns = new List();
//鼠标移动到行上时的颜色
private Color cell_color = Utilities.PublicResource.YkdBasisColors.LightBlueColor;//Color.FromArgb(209, 244, 244);
//是否画三解
private bool shifouhuasanjiao = false;
private Color color_grid = Color.FromArgb(236, 233, 216);
#region
#endregion

    bool click = false;
    public UCDataGridView()
    {
        //this.DefaultCellStyle.SelectionBackColor = Color.FromArgb(209, 244, 244);
        this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
        UpdateStyles();
    }
    protected override void OnCreateControl()
    {

        this.EnableHeadersVisualStyles = false;
        //this.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(236, 233, 216);
        this.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
        //列头背景色,字体加粗
        this.EnableHeadersVisualStyles = false;
        this.ColumnHeadersDefaultCellStyle.BackColor = Utilities.PublicResource.YkdBasisColors.GrayBackColorF7;
        this.ColumnHeadersDefaultCellStyle.Font = MIP.Utilities.PublicResource.YkdTextFonts.TextFontBold;

        //this.ColumnHeadersHeight = 20;
        this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
        this.ColumnHeadersDefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
        this.ColumnHeadersDefaultCellStyle.ForeColor = Color.Black;
        //this.ColumnHeadersDefaultCellStyle.SelectionBackColor = System.Drawing.SystemColors.Highlight;
        //this.ColumnHeadersDefaultCellStyle.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
        //this.RowHeadersDefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
        this.RowHeadersDefaultCellStyle.BackColor = Color.FromArgb(236, 233, 216);
        this.RowHeadersDefaultCellStyle.ForeColor = System.Drawing.SystemColors.WindowText;
        this.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
        //this.DefaultCellStyle.SelectionBackColor = Color.DarkBlue;
        //this.DefaultCellStyle.SelectionForeColor = Color.DarkSlateBlue;
        this.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
        //this.GridColor = Color.Silver;//表格点击后颜色 表格线颜色
        this.BackgroundColor = System.Drawing.SystemColors.Window;
        this.BorderStyle = System.Windows.Forms.BorderStyle.None;
        this.AllowUserToOrderColumns = true;
        this.AutoGenerateColumns = true;
        base.OnCreateControl();
    }
    public int RowIndex = -1;

    public int CurrentRowIndexForMa = 0;
    Color defaultcolor;
    //移到单元格时的颜色
    protected override void OnCellMouseMove(DataGridViewCellMouseEventArgs e)
    {
        try
        {
            if (e.RowIndex < 0)
                return;
            if (_CellColorOnchange)
            {
                if (e.RowIndex == RowIndex)
                {
                    Rows[e.RowIndex].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#006581");
                }
                else
                    Rows[e.RowIndex].DefaultCellStyle.BackColor = cell_color;
            }
            //Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "OnCellMouseMove" + Rows[e.RowIndex].DefaultCellStyle.BackColor);
            //System.Threading.Thread.Sleep(100);
            foreach (UCDataGridViewImageAndDataModel item in this.IconColumnList)
            {
                //在这里可以根据数据判断ICON图标是否显示
                if (this.Columns[e.ColumnIndex].Name == item.ColumnName)
                {
                    if (e.RowIndex < 0)
                        return;

                    //数据与图片列
                    UCDataGridViewImageAndDataCell imageAndDataCell = this.Rows[e.RowIndex].Cells[e.ColumnIndex] as UCDataGridViewImageAndDataCell;
                    if (imageAndDataCell != null)   //判断当前是不是按钮列
                    {
                        if (imageAndDataCell.IsOnIconImage(null, e))
                        {
                            this.OnIconImageName = imageAndDataCell.OnIconImageName;
                        }
                    }
                }
            }

        }
        catch (Exception ex)
        {
            //throw ex;
        }
        base.OnCellMouseMove(e);
    }
    //进入单元格时保存当前的颜色
    protected override void OnCellMouseEnter(DataGridViewCellEventArgs e)
    {
        base.OnCellMouseEnter(e);
        if (e.RowIndex < 0)
            return;
        try
        {
            if (_CellColorOnchange)
                if (e.RowIndex == RowIndex)
                {
                    this.Rows[e.RowIndex].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#006581");
                }
                else
                    defaultcolor = Rows[e.RowIndex].DefaultCellStyle.BackColor;


        }
        catch (Exception ex)
        {
            throw ex;
        }
        //Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "OnCellMouseEnter" + Rows[e.RowIndex].DefaultCellStyle.BackColor);
        //System.Threading.Thread.Sleep(100);

    }
    //离开时还原颜色
    protected override void OnCellMouseLeave(DataGridViewCellEventArgs e)
    {
        base.OnCellMouseLeave(e);
        try
        {
            if (e.RowIndex < 0)
                return;
            if (_CellColorOnchange)
            {
                //Rows[e.RowIndex].DefaultCellStyle.ForeColor = this.RowsDefaultCellStyle.ForeColor;
                for (int i = 0; i < Rows.Count; i++)
                {
                    if (i != e.RowIndex)
                    {

                    }


                    if (i == RowIndex)
                    {
                        Rows[i].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#006581");
                    }
                    else if (i == e.RowIndex)
                    {
                        Rows[i].DefaultCellStyle.BackColor = defaultcolor;
                    }
                    else if (i % 2 == 1)
                    {
                        Rows[i].DefaultCellStyle.BackColor = this.AlternatingRowsDefaultCellStyle.BackColor;
                    }
                    else
                    {
                        Rows[i].DefaultCellStyle.BackColor = this.RowsDefaultCellStyle.BackColor;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        //Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "OnCellMouseLeave" + Rows[e.RowIndex].DefaultCellStyle.BackColor);
        //System.Threading.Thread.Sleep(100);

    }



    /// <summary>
    /// 自定义列
    /// </summary>
    [Bindable(false), Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public List<DataGridViewColumn> CUColumns
    {
        get { return _columns; }
        set
        {
            _columns = value;
            LoadColumns();
        }
    }
    private List<UCDataGridViewColumn> _orderColumns = new List<UCDataGridViewColumn>();
    [Bindable(false), Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public List<UCDataGridViewColumn> OrderColumns
    {
        get { return _orderColumns; }
        set { _orderColumns = value; }
    }

    /// <summary>
    /// 加载列
    /// </summary>
    private void LoadColumns()
    {
        try
        {
            this.Columns.Clear();
            if (OrderColumns.Count > 0 && OrderColumns.Count == _columns.Count)
            {
                foreach (UCDataGridViewColumn _orderItem in OrderColumns.OrderBy(x => x.OrderSort))
                {
                    foreach (DataGridViewColumn item in _columns)
                    {
                        if (item.Name == _orderItem.Column.Name)
                        {
                            this.Columns.Add(item);
                            break;
                        }

                    }
                }
            }
            else
            {
                foreach (DataGridViewColumn item in _columns)
                {

                    this.Columns.Add(item);
                }
            }
        }
        finally
        {
        }
    }
    /// <summary>
    /// 鼠标移动进来改变颜色 外部暴露属性
    /// </summary>
    [Description("获取或设置 鼠标移动进来改变颜色"), Category("自定义")]
    public bool CellColorOnchange
    {
        get
        {
            return _CellColorOnchange;
        }
        set
        {
            _CellColorOnchange = value;
        }
    }
    /// <summary>
    /// 默认颜色设置
    /// </summary>
    public Color DefaultcolorSet
    {
        get
        {
            return cell_color;
        }
        set
        {
            cell_color = value;
        }
    }
    public bool Shifouhua_Sanjiao
    {
        get
        {
            return shifouhuasanjiao;
        }
        set
        {
            shifouhuasanjiao = value;
        }
    }

    #region 各列所需属性
    private List<UCDataGridViewActionButtonModel> _buttons = new List<UCDataGridViewActionButtonModel>();
    /// <summary>
    /// 图片按钮
    /// </summary>
    [Bindable(false), Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public List<UCDataGridViewActionButtonModel> ButtonsList
    {
        get { return _buttons; }
        set
        {
            _buttons = value;
            //loadButton();
        }
    }

    public int ClickButtonIndex = -1;

    public string OnIconImageName { get; set; }

    private List<UCDataGridViewImageAndDataModel> _iconImageList = new List<UCDataGridViewImageAndDataModel>();
    /// <summary>
    /// icon图片List
    /// </summary>
    [Bindable(false), Browsable(fals
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值