1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Reflection; 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 SetDgvColTextByColName(DataGridView dgv, string colName, string headerText) { if (headerText.EndsWith( ":" ) || headerText.EndsWith( ":" )) { headerText = headerText.Replace( ":" , "" ); headerText = headerText.Replace( ":" , "" ); } dgv.Columns[colName].HeaderText = headerText; } 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 ; } public Color StrAGBToColor( string agb) { string [] colors = agb.Split( ',' ); return Color.FromArgb( int .Parse(colors[0]), int .Parse(colors[1]), int .Parse(colors[2])); } /// <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); // 改变第一列列头内容 dgv.EnableHeadersVisualStyles = false ; //允许更改行、列头的字体颜色 dgv.TopLeftHeaderCell.Value = dgv.RowCount.ToString(); dgv.TopLeftHeaderCell.Style.ForeColor = Color.Salmon; dgv.TopLeftHeaderCell.Style.Font = new Font( "微软雅黑" , 10, FontStyle.Bold); dgv.TopLeftHeaderCell.ToolTipText = "查询数据总量为:" + dgv.RowCount.ToString() + "(条)" ; } catch { return ; } } public void SetDgvNumCloByRowStateChanged(DataGridView dgv, DataGridViewRowStateChangedEventArgs e) { int rowCounts = dgv.Rows.Count; // 改变第一列列头内容 if (rowCounts == 0) dgv.TopLeftHeaderCell.Value = string .Empty; } } #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 } |
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)