Created
March 16, 2012 23:24
-
-
Save gabrielgreen/2053553 to your computer and use it in GitHub Desktop.
asp.net grid view extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Globalization; | |
using System.Web.UI.WebControls; | |
namespace ClientPortal | |
{ | |
public static class Extensions | |
{ | |
/// <extends><c>GridView</c></extends> | |
/// <summary> | |
/// </summary> | |
/// <param name="row"></param> | |
/// <param name="field"></param> | |
/// <returns>The <c>TableCell</c> of the column bound to the field named <paramref name="field"/></returns> | |
public static TableCell GetCell(this GridViewRow row, string field) | |
{ | |
var gv = (GridView)row.NamingContainer; | |
for (int columnIndex = 0; columnIndex < gv.Columns.Count; columnIndex++) | |
{ | |
var boundField = gv.Columns[columnIndex] as BoundField; | |
if (boundField != null && boundField.DataField == field) | |
{ | |
return row.Cells[columnIndex]; | |
} | |
var templateField = gv.Columns[columnIndex] as TemplateField; | |
if (templateField != null && templateField.HeaderText == field) | |
{ | |
return row.Cells[columnIndex]; | |
} | |
} | |
throw new Exception("Could not find index of " + field); | |
} | |
/// <summary> | |
/// </summary> | |
/// <param name="row"></param> | |
/// <param name="field"></param> | |
/// <returns></returns> | |
public static string GetCellText(this GridViewRow row, string field) | |
{ | |
TableCell cell = row.GetCell(field); | |
if (cell.Controls.Count > 0) | |
{ | |
if (cell.Controls[1] is Label) | |
{ | |
return (cell.Controls[1] as Label).Text; | |
} | |
} | |
return cell.Text; | |
} | |
/// <summary> | |
/// </summary> | |
/// <param name="row"></param> | |
/// <param name="field"></param> | |
/// <param name="text"></param> | |
/// <returns></returns> | |
public static void SetCellText(this GridViewRow row, string field, string text) | |
{ | |
try | |
{ | |
row.GetCell(field).Text = text; | |
} | |
catch | |
{ | |
// setting the text of a non existant row has no effect | |
} | |
} | |
/// <summary> | |
/// </summary> | |
/// <param name="row"></param> | |
/// <param name="field"></param> | |
/// <param name="func"></param> | |
public static void ChangeCellText(this GridViewRow row, string field, Func<string, string> func) | |
{ | |
string toText = func(row.GetCell(field).Text); | |
if(string.IsNullOrWhiteSpace(toText)) | |
{ | |
toText = _EmptyCellText; | |
} | |
row.SetCellText(field, toText); | |
} | |
/// <extends>decimal</extends> | |
/// <summary> | |
/// </summary> | |
/// <param name="amount"></param> | |
/// <param name="currency"></param> | |
/// <returns>A string representation of the decimal <paramref name="amount"/> | |
/// formatted as the <paramref name="currency"/>.</returns> | |
public static string ToCurrency(this decimal amount, string currency) | |
{ | |
if (!_CurrencyMap.ContainsKey(currency)) | |
{ | |
throw new Exception("Unsupported target currency: " + currency); | |
} | |
if (amount == 0) | |
{ | |
return _EmptyCellText; | |
} | |
else | |
{ | |
CultureInfo culture = CultureInfo.CreateSpecificCulture(_CurrencyMap[currency]); | |
return string.Format(culture, "{0:C}", amount); | |
} | |
} | |
/// <summary> | |
/// Trims off trailing zeros and decimal points. | |
/// </summary> | |
/// <param name="number"></param> | |
/// <returns></returns> | |
public static string ToNumber(this string number) | |
{ | |
return number.TrimEnd('.', '0'); | |
} | |
static string _EmptyCellText = "-"; | |
static Dictionary<string, string> _CurrencyMap = new Dictionary<string, string> | |
{ | |
{"USD", "en-us"}, | |
{"GBP", "en-gb"}, | |
{"EUR", "de-de"}, | |
{"AUD", "en-AU"}, | |
{"CAD", "en-us"}, | |
// add additional currencies here | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment