/******************************
* 说明: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