/****************************** * 说明:DataTable相关扩展方法 * 创建人:龚宇超 * 创建日期:2017-11-15 * 修改人: * 修改日期: * 修改备注: * 版本:1.0.0.0 ******************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Collections; using System.Threading.Tasks; namespace Lskj.Util { /// /// DataTable相关扩展方法 /// public static class DataTableExtend { public static TaskSchedulerUtil SchedulerUtil = new TaskSchedulerUtil(30); /// /// 说明:移除DataTable中DataRow空白行 /// 创建人:龚宇超 /// 创建日期:2017-11-15 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The rows. public static DataTable TrimEmptyRows(this DataTable table) { //GridView.DeleteSelectedRows()只是改变了数据状态为delete,数据依然存在于table中需要更新 table.AcceptChanges(); if (table == null) return table; int rowCount = table.Rows.Count; for (int i = rowCount - 1; i > 0; i--) { bool isNull = true; DataRow item = table.Rows[i]; for (int j = 0; j < table.Columns.Count; j++) { DataColumn col = table.Columns[j]; if (!string.IsNullOrWhiteSpace(item[col.ColumnName] + "")) { isNull = false; break; } } if (isNull) { table.Rows.Remove(item); } } return table; } /// /// 说明:移除DataTable中已删除的行 /// 创建人:龚宇超 /// 创建日期:2017-11-30 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The table. /// DataTable. public static DataTable TrimDeleteRows(this DataTable table) { if (table == null || table.Rows.Count == 0) return table; DataRow[] deleteRows = table.Rows.Cast().Where(x => x.RowState == DataRowState.Deleted).ToArray(); for (int i = 0; i < deleteRows.Length; i++) { table.Rows.Remove(deleteRows[i]); } return table; } /// /// 说明:按field排序DataTable /// 创建人:龚宇超 /// 创建日期:2017-11-30 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The table. /// The field. /// DataTable. public static DataTable OrderBy(this DataTable table, string field, bool isAccept = true) { if (table == null || table.Rows.Count == 0 || string.IsNullOrEmpty(field) || !table.Columns.Contains(field)) return table; if (isAccept) table.AcceptChanges(); table = table.TrimDeleteRows(); for (int i = 0; i < table.Rows.Count; i++) { table.Rows[i][field] = (i + 1); } return table; } public static DataTable OrderBy(this DataTable table, string field, int startIndex, int endIndex) { if (table == null || table.Rows.Count == 0 || string.IsNullOrEmpty(field) || !table.Columns.Contains(field) || startIndex < 0) return table; table.AcceptChanges();//此处提交修改会有问题,后续保存时无法获取多次修改的状态 table = table.TrimDeleteRows(); //for (int i = 0; i < table.Rows.Count; i++) //{ // table.Rows[i][field] = (i + 1); //} //int startUpdateIndex = 0; //int endUpdateIndex = 0; //if (endIndex < 0) //{ // startUpdateIndex = startIndex; // endUpdateIndex = table.Rows.Count - 1; //} //else //{ // startUpdateIndex = startIndex < endIndex ? startIndex : endIndex; // endUpdateIndex = startIndex < endIndex ? endIndex : startIndex; //} //startUpdateIndex = selectSourceIndex != null && selectSourceIndex.Length > 0 ? startUpdateIndex < selectSourceIndex.Min() ? startUpdateIndex : selectSourceIndex.Min() : startUpdateIndex; //endUpdateIndex = selectSourceIndex != null && selectSourceIndex.Length > 0 ? endUpdateIndex > selectSourceIndex.Max() ? endUpdateIndex : selectSourceIndex.Max() : endUpdateIndex; //每次拖动排序后都会改变行状态,导致保存时只会保存最后拖动改变的行,改为全部排序。 int startUpdateIndex = 0; int endUpdateIndex = table.Rows.Count - 1; for (int i = startUpdateIndex; i <= endUpdateIndex; i++) { table.Rows[i][field] = i + 1; } return table; } /// /// 说明:按配置条件排序DataTable /// 创建人:王一帆 /// 创建日期:2020-07-03 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The table. /// The field. /// DataTable. public static DataTable OrderBy(this DataTable table, string field, string UpperFieldName, int RowHandle) { if (table == null || table.Rows.Count == 0 || string.IsNullOrEmpty(field) || !table.Columns.Contains(field)) return table; table.AcceptChanges(); table = table.TrimDeleteRows(); if (RowHandle == 0) { for (int i = RowHandle; i < table.Rows.Count; i++) { if (string.IsNullOrEmpty(table.Rows[i][UpperFieldName] + "")) continue; table.Rows[i + 1][field] = table.Rows[i][UpperFieldName]; } } else { for (int i = RowHandle; i < table.Rows.Count; i++) { if (string.IsNullOrEmpty(table.Rows[i - 1][UpperFieldName] + "")) continue; table.Rows[i][field] = table.Rows[i - 1][UpperFieldName]; } } return table; } /// /// 获取HashTable值 /// /// /// /// /// /// /// public static bool GetValue(this Dictionary dic, object control, string key, out T value) { try { if (dic == null) { value = default; return false; } if (dic.ContainsKey(control)) { if (dic[control].ContainsKey(key)) { Task task = dic[control][key] as Task; value = task.Result; task.Dispose(); return true; } else { value = default; return false; } } else { value = default; return false; } } catch (Exception ex) { Console.WriteLine(ex.Message); value = default; return false; } } /// /// 获得任务 /// /// /// /// /// /// public static Task GetTask(this Dictionary cachesDic, object control, string key) { try { if (cachesDic.ContainsKey(control) && cachesDic[control].ContainsKey(key)) { return (Task)cachesDic[control][key]; } else { return default; } } catch (Exception ex) { Console.WriteLine(ex.Message); return default; } } /// /// 添加任务 /// /// /// /// /// /// public static Task AddTask(this Dictionary cachesDic, object control, string key, Task task) { Hashtable cachesHash = new Hashtable(); if (!cachesDic.ContainsKey(control)) { cachesDic.Add(control, cachesHash); } else { cachesHash = cachesDic[control]; } cachesHash.Add(key, task); if (task.Status == TaskStatus.Created) { task.Start(SchedulerUtil); } return task; } } }