ab56a9bcf7
SVN-Revision: r240
147 lines
5.2 KiB
C#
147 lines
5.2 KiB
C#
/******************************
|
|
* 说明:DataTable相关扩展方法
|
|
* 创建人:龚宇超
|
|
* 创建日期:2017-11-15
|
|
* 修改人:
|
|
* 修改日期:
|
|
* 修改备注:
|
|
* 版本:1.0.0.0
|
|
******************************/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Data;
|
|
|
|
namespace Lskj.Util
|
|
{
|
|
/// <summary>
|
|
/// DataTable相关扩展方法
|
|
/// </summary>
|
|
public static class DataTableExtend
|
|
{
|
|
/// <summary>
|
|
/// <para>说明:移除DataTable中DataRow空白行</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-11-15 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="rows">The rows.</param>
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:移除DataTable中已删除的行</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-11-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="table">The table.</param>
|
|
/// <returns>DataTable.</returns>
|
|
public static DataTable TrimDeleteRows(this DataTable table)
|
|
{
|
|
if (table == null || table.Rows.Count == 0) return table;
|
|
|
|
DataRow[] deleteRows = table.Rows.Cast<DataRow>().Where(x => x.RowState == DataRowState.Deleted).ToArray();
|
|
|
|
for (int i = 0; i < deleteRows.Length; i++)
|
|
{
|
|
table.Rows.Remove(deleteRows[i]);
|
|
}
|
|
return table;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:按field排序DataTable</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-11-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="table">The table.</param>
|
|
/// <param name="field">The field.</param>
|
|
/// <returns>DataTable.</returns>
|
|
public static DataTable OrderBy(this DataTable table, string field)
|
|
{
|
|
if (table == null || table.Rows.Count == 0 || string.IsNullOrEmpty(field) || !table.Columns.Contains(field)) return table;
|
|
|
|
table.AcceptChanges();
|
|
table = table.TrimDeleteRows();
|
|
|
|
for (int i = 0; i < table.Rows.Count; i++)
|
|
{
|
|
table.Rows[i][field] = (i + 1);
|
|
}
|
|
|
|
return table;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:按配置条件排序DataTable</para>
|
|
/// <para>创建人:王一帆</para>
|
|
/// <para>创建日期:2020-07-03 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="table">The table.</param>
|
|
/// <param name="field">The field.</param>
|
|
/// <returns>DataTable.</returns>
|
|
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;
|
|
}
|
|
}
|
|
}
|