using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
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 TModel GetCurrentRowModelObj<TModel>(DataGridView dgv)
        {
            return (TModel)dgv.CurrentRow.DataBoundItem;
        }

        public List<DataGridViewRow> GetSelectRowByColName(DataGridView dgv, string colName)
        {
            if (dgv.Rows.Count == 0) return null;
            var checkedRows = from DataGridViewRow row in dgv.Rows
                              where (bool)row.Cells[colName].Value//(bool)row.Cells["ColSelect"].Value
                              select row;
            List<DataGridViewRow> temp = checkedRows as List<DataGridViewRow>;
            return temp;
        }

        public DgvSortHelper<T> GetSorDataSource<T>(List<T> list)
        {
            DgvSortHelper<T> temps = new DgvSortHelper<T>();
            foreach (T item in list)
            {
                temps.Add(item);
            }
            return temps;
        }

        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; }
        }

    }

    #region DgvSortHelper
    public class DgvSortHelper<T> : BindingList<T>
    {
        private bool isSortedCore = true;
        private ListSortDirection sortDirectionCore = ListSortDirection.Ascending;
        private PropertyDescriptor sortPropertyCore = null;
        private string defaultSortItem;

        public DgvSortHelper() : base() { }

        public DgvSortHelper(IList<T> list) : base(list) { }

        protected override bool SupportsSortingCore
        {
            get { return true; }
        }

        protected override bool SupportsSearchingCore
        {
            get { return true; }
        }

        protected override bool IsSortedCore
        {
            get { return isSortedCore; }
        }

        protected override ListSortDirection SortDirectionCore
        {
            get { return sortDirectionCore; }
        }

        protected override PropertyDescriptor SortPropertyCore
        {
            get { return sortPropertyCore; }
        }

        protected override int FindCore(PropertyDescriptor prop, object key)
        {
            for (int i = 0; i < this.Count; i++)
            {
                if (Equals(prop.GetValue(this[i]), key)) return i;
            }
            return -1;
        }

        protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
        {
            isSortedCore = true;
            sortPropertyCore = prop;
            sortDirectionCore = direction;
            Sort();
        }

        protected override void RemoveSortCore()
        {
            if (isSortedCore)
            {
                isSortedCore = false;
                sortPropertyCore = null;
                sortDirectionCore = ListSortDirection.Ascending;
                Sort();
            }
        }

        public string DefaultSortItem
        {
            get { return defaultSortItem; }
            set
            {
                if (defaultSortItem != value)
                {
                    defaultSortItem = value;
                    Sort();
                }
            }
        }

        private void Sort()
        {
            List<T> list = (this.Items as List<T>);
            list.Sort(CompareCore);
            ResetBindings();
        }

        private int CompareCore(T o1, T o2)
        {
            int ret = 0;
            if (SortPropertyCore != null)
            {
                ret = CompareValue(SortPropertyCore.GetValue(o1), SortPropertyCore.GetValue(o2), SortPropertyCore.PropertyType);
            }
            if (ret == 0 && DefaultSortItem != null)
            {
                PropertyInfo property = typeof(T).GetProperty(DefaultSortItem, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.IgnoreCase, null, null, new Type[0], null);
                if (property != null)
                {
                    ret = CompareValue(property.GetValue(o1, null), property.GetValue(o2, null), property.PropertyType);
                }
            }
            if (SortDirectionCore == ListSortDirection.Descending) ret = -ret;
            return ret;
        }

        private static int CompareValue(object o1, object o2, Type type)
        {
            if (o1 == null) return o2 == null ? 0 : -1;
            else if (o2 == null) return 1;
            else if (type.IsPrimitive || type.IsEnum) return Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2));
            else if (type == typeof(DateTime)) return Convert.ToDateTime(o1).CompareTo(o2);
            else return String.Compare(o1.ToString().Trim(), o2.ToString().Trim());
        }
    }
    #endregion

}

  

原文地址:http://www.cnblogs.com/YYkun/p/16836989.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性