using DevExpress.Spreadsheet;
using DeviceRepairAndOptimization.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
namespace DeviceRepairAndOptimization.Utils
{
public static class PublicExtended
{
///
/// 判断DataSet总是否有数据
///
///
///
public static bool HasData(this DataSet ds)
{
if (ds != null && ds.Tables.Count > 0)
{
return true;
}
return false;
}
///
/// 判断DataTable总是否有数据
///
///
///
public static bool HasData(this DataTable table)
{
if (table != null && table.Rows.Count > 0)
{
return true;
}
return false;
}
///
/// 判断集合是否为空
///
///
///
///
public static bool IsNullOrZero(this List lst) where T : class
{
if (lst == null || lst.Count == 0)
{
return true;
}
return false;
}
///
/// 从集合中随机取出一个对象
///
///
///
///
public static T RandomOne(this List lst) where T : class
{
if (lst != null && lst.Count > 0)
{
return lst.OrderBy(x => Guid.NewGuid()).First();
}
return null;
}
public static DataTable toDataTable(this List lst, string TableName = null) where T : class
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
if (!string.IsNullOrEmpty(TableName))
table.TableName = TableName;
foreach (PropertyDescriptor prop in properties)
{
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
}
foreach (T item in lst)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
{
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
}
table.Rows.Add(row);
}
return table;
}
public static void WriteValue(this List lst, Cell cell)
{
try
{
SheetDataItem item = lst.First(x => x.ColumnIndex == cell.ColumnIndex && x.RowIndex == cell.RowIndex);
if (item != null)
item.Value = cell.Value.TextValue;
}
catch (Exception ex)
{
XtraMessageBoxHelper.Error(ex.Message);
}
}
}
}