/// <summary> /// Formats a specified column in a DataTable with one of the pre-specified formats. These will only work in string /// data type fields. /// Todo: Could the data.formatting functions be exposed as extensions on Object or String? /// </summary> /// <param name="dt"></param> /// <param name="columnIndex"></param> /// <param name="formatType"></param> public static DataTable FormatColumn(this DataTable dt, int columnIndex, FormatFieldTypes formatType) { // The main rows/cells foreach (DataRow row in dt.Rows) { switch (formatType) { case FormatFieldTypes.Dollars: row[columnIndex] = Formatting.FormatDollars(row[columnIndex].ToString()); break; case FormatFieldTypes.EmailLink: row[columnIndex] = Formatting.ReturnMailLink(row[columnIndex].ToString()); break; case FormatFieldTypes.Link: row[columnIndex] = Formatting.ReturnLinkHtml(row[columnIndex].ToString(), true, true); break; case FormatFieldTypes.CleanupNumber: row[columnIndex] = Formatting.CleanupNumber(row[columnIndex].ToString()); break; case FormatFieldTypes.NumberWithFormattingNoDecimal: row[columnIndex] = row[columnIndex].ToString().FormatIfNumber(0); break; case FormatFieldTypes.NumberWithFormattingTwoDecimals: row[columnIndex] = row[columnIndex].ToString().FormatIfNumber(2); break; case FormatFieldTypes.PercentWithNoDecimal: row[columnIndex] = Formatting.FormatPercent(row[columnIndex].ToString(), 0); break; case FormatFieldTypes.PercentWithTwoDecimals: row[columnIndex] = Formatting.FormatPercent(row[columnIndex].ToString(), 2); break; case FormatFieldTypes.PhoneNumber: row[columnIndex] = Formatting.FormatPhoneNumber(row[columnIndex].ToString()); break; case FormatFieldTypes.ZipCode: row[columnIndex] = Formatting.FormatZipCode(row[columnIndex].ToString()); break; case FormatFieldTypes.ShortDate: DateTime asDate; if (DateTime.TryParse(row[columnIndex].ToString(), out asDate)) { row[columnIndex] = asDate.ToString("MM/dd/yyyy"); } break; } } return(dt); }