基线 SVN r240
SVN-Revision: r240
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
/******************************
|
||||
* 说明: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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Data;
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Lskj.Util
|
||||
{
|
||||
public static class ListExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>说明:集合转为DataTable</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-06-11 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="collection">The collection.</param>
|
||||
/// <returns>DataTable.</returns>
|
||||
public static DataTable ToDataTable<T>(this List<T> list)
|
||||
{
|
||||
IEnumerable<T> collection= list.AsEnumerable();
|
||||
var props = typeof(T).GetProperties();
|
||||
var dt = new DataTable();
|
||||
dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
|
||||
if (collection.Count() > 0)
|
||||
{
|
||||
for (int i = 0; i < collection.Count(); i++)
|
||||
{
|
||||
ArrayList tempList = new ArrayList();
|
||||
foreach (PropertyInfo pi in props)
|
||||
{
|
||||
object obj = pi.GetValue(collection.ElementAt(i), null);
|
||||
tempList.Add(obj);
|
||||
}
|
||||
object[] array = tempList.ToArray();
|
||||
dt.LoadDataRow(array, true);
|
||||
}
|
||||
}
|
||||
return dt;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/******************************
|
||||
* 说明:字符串扩展
|
||||
* 创建人:龚宇超
|
||||
* 创建日期:2018-05-28
|
||||
* 修改人:
|
||||
* 修改日期:
|
||||
* 修改备注:
|
||||
* 版本:1.0.0.0
|
||||
******************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Data;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Lskj.Util
|
||||
{
|
||||
public static class StringExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>说明:字符串转换为DataTable</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-05-28 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="stringData">The string data.</param>
|
||||
/// <param name="columns">The columns.</param>
|
||||
/// <returns>DataTable.</returns>
|
||||
public static DataTable ToNewDataTable(this string stringData,string columns)
|
||||
{
|
||||
DataTable dtTable = new DataTable();
|
||||
stringData = stringData.Replace(" ", "").Replace(",,", ",").Trim(',');
|
||||
string[] confirmOper = stringData.Split(',');
|
||||
for (int i = 0; i < confirmOper.Length; i++)
|
||||
{
|
||||
DataRow row = dtTable.NewRow();
|
||||
row[columns] = confirmOper[i];
|
||||
dtTable.Rows.Add(row);
|
||||
}
|
||||
return dtTable;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>说明:</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-10-23 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="s">The s.</param>
|
||||
/// <param name="format">The format.</param>
|
||||
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
|
||||
public static string ToStr(this object s, string format = "")
|
||||
{
|
||||
string result = string.Empty;
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(format))
|
||||
{
|
||||
result = s.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
result = string.Format("{0:" + format + "}", s);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:{key}字符串替换</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-05-28 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <param name="ignoreCase">是否忽略大小写</param>
|
||||
/// <returns>System.String.</returns>
|
||||
public static string Replace(this string str, string key, string value, bool ignoreCase)
|
||||
{
|
||||
if (string.IsNullOrEmpty(str)) return str;
|
||||
|
||||
return Regex.IsMatch(str, key, RegexOptions.IgnoreCase) ?
|
||||
System.Text.RegularExpressions.Regex.Replace(str, key, value, ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None)
|
||||
: str;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期: </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
public static bool IsChinese(this string text)
|
||||
{
|
||||
for (int i = 0; i < text.Length; i++)
|
||||
{
|
||||
if ((int)text[i] > 127)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user