c# 常用扩展方法
DataTable
public static class DataTableExtension
{
public static T GetData<T>(this DataTable dt, int rowIndex, string colName)
{
if (dt == null || dt.Rows.Count == 0)
{
return default(T);
}
//判断是否超过行数
if (dt.Rows.Count <= (rowIndex + 1))
{
return default(T);
}
var row = dt.Rows[rowIndex];
var cell = row[colName];
if (cell == null)
{
return default(T);
}
try
{
var data = (T)cell;
return data;
}
catch
{
return default (T);
}
}
}