You need to create the column as a TemplateColumn
TemplateColumn currencyColumn= new TemplateColumn();
currencyColumn.HeaderText = "$ Amount";
currencyColumn.ItemTemplate = new CurrencyColumnTemplate("moneyfield");
currencyColumn.EditItemTemplate = new CurrencyColumnTemplate("moneyfield");
You have to create the CurrencyColumn type that implements your custom
rendering logic:
public class CurrencyColumnTemplate : System.Web.UI.ITemplate {
string m_DataTextField;
public CurrencyColumnTemplate(string dataTextField) {
m_DataTextField = dataTextField;
}
private void BindData(object sender, EventArgs e){
Label label = (Label) sender;
DataGridItem container = (DataGridItem) label.NamingContainer;
DataRowView curRow = (DataRowView) container.DataItem;
// this is where you would add your logic to emit a different style
// for the label, depending on the value of curRow[m_DataTextField]
label.Text = curRow[m_DataTextField].ToString();
}
#region ITemplate Members
public void InstantiateIn(System.Web.UI.Control container){
Label label = new Label();
label.DataBinding += new EventHandler(BindData);
container.Controls.Add(label);
}
#endregion
#region Property accessors
string DataTextField {
get { return m_DataTextField; }
set { m_DataTextField = value; }
}
#endregion
}
If that isn't thorough enough, try searching the web for custome
TemplateColumns and ITemplate.
Joshua Flanagan
http://flimflan.com/blog
Phil wrote:
Using C#, I am building my datagrid in my code behind due to the need to
dynamically build the columns. In my Code I use an array to accomplish this
and I also use an if statement to determine if the column being built is
called "$ Amount". If it hits this column, I use the DataFormatString =
"{0:N}"
All numbers are coming accross fine, if they are posative. For negative I
need them to be the color red when displayed in the datagrid. I hope this is
enough info for someone to help me. It seems like a simple task, but I can
not figure out how to make it work.
Thanks,
Phil