/// <summary> /// Convert cell value to DateTime /// </summary> /// <param name="dataRow"></param> /// <param name="columnName">we will use dataRow[0] instead of empty columnName</param> /// <param name="defaultVal">the value should return if convert is failed or your data row value is DBNull</param> /// <returns></returns> public static DateTime ToDateTime(this DataRow dataRow, string columnName, DateTimeDefaultReturn defaultDtVal = DateTimeDefaultReturn.Now) { if (dataRow == null) { throw new ArgumentNullException("dataRow"); } DateTime dtNow = DateTime.Now; if (defaultDtVal == DateTimeDefaultReturn.Min) { dtNow = DateTime.MinValue; } if (defaultDtVal == DateTimeDefaultReturn.Max) { dtNow = DateTime.MaxValue; } string result = string.Empty; if (!string.IsNullOrEmpty(columnName)) { result = GlobalExt.ObjectToString(dataRow[columnName]); } else { result = GlobalExt.ObjectToString(dataRow[0]); } if (!string.IsNullOrEmpty(result)) { DateTime.TryParse(result, out dtNow); return(dtNow); } return(DateTime.Now); }
/// <summary> /// Convert cell value to bool /// </summary> /// <param name="dataRow">data row value could be: "True","true","False","false",0,1,or any other numbers</param> /// <param name="columnName">we will use dataRow[0] instead of empty columnName</param> /// <param name="defaultVal">the value should return if convert is failed or your data row value is DBNull</param> /// <returns>return ture if data row value is numberic and greater than 0,otherwise return false</returns> public static bool ToBoolean(this DataRow dataRow, string columnName, bool defaultVal = false) { if (dataRow == null) { throw new ArgumentNullException("dataRow"); } bool result = defaultVal; if (!string.IsNullOrEmpty(columnName)) { return(GlobalExt.ObjectToBoolean(dataRow[columnName])); } else { return(GlobalExt.ObjectToBoolean(dataRow[0])); } }
/// <summary> /// Convert cell value to uInt16 /// </summary> /// <param name="dataRow"></param> /// <param name="columnName">we will use dataRow[0] instead of empty columnName</param> /// <param name="defaultVal">the value should return if convert is failed or your data row value is DBNull</param> /// <returns></returns> public static ushort ToUInt16(this DataRow dataRow, string columnName, ushort defaultVal = 0) { if (dataRow == null) { throw new ArgumentNullException("dataRow"); } ushort result = defaultVal; if (!string.IsNullOrEmpty(columnName)) { result = GlobalExt.ObjectToUshort(dataRow[columnName], defaultVal); } else { result = GlobalExt.ObjectToUshort(dataRow[0], defaultVal); } return(result); }
/// <summary> /// Convert cell value to string /// </summary> /// <param name="dataRow"></param> /// <param name="columnName">we will use dataRow[0] instead of empty columnName</param> /// <param name="defaultVal">the value should return if convert is failed or your data row value is DBNull</param> /// <returns></returns> public static string ToString(this DataRow dataRow, string columnName, string defaultVal = "") { if (dataRow == null) { throw new ArgumentNullException("dataRow"); } string result = defaultVal; if (!string.IsNullOrEmpty(columnName)) { result = GlobalExt.ObjectToString(dataRow[columnName]); } else { result = GlobalExt.ObjectToString(dataRow[0]); } return(result); }