Format Negative Currency in Datagrid

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
Thanks.. But actually, I found a work around to what I needed. I created a
new Function and it reads the column values and sets a ForeColor on values
less than 0

private void DGRequests_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)

{
double rowVal;
if (e.Item.ItemType == System.Web.UI.WebControls.ListItemType.Item ||
e.Item.ItemType == System.Web.UI.WebControls.ListItemType.AlternatingItem)
{
try
{
rowVal = Convert.ToDouble(e.Item.Cells[ 3 ].Text);
if (rowVal < 0)
{
e.Item.Cells[ 3 ].ForeColor = Color.Red;
}else{
e.Item.Cells[ 3 ].ForeColor = Color.Black;
}
}

catch(Exception r){
r.ToString();
}
}
}



Joshua Flanagan said:
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


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
 
Ha, great. It felt like I was giving an overly-complicated solution for
the problem, but I wasn't thinking clearly. Glad you found better guidance.
However, I would recommend getting familiar with the TemplateColumn and
creating your own ITemplate classes - they come in handy for more
complicated scenarios.

Thanks.. But actually, I found a work around to what I needed. I created a
new Function and it reads the column values and sets a ForeColor on values
less than 0

private void DGRequests_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)

{
double rowVal;
if (e.Item.ItemType == System.Web.UI.WebControls.ListItemType.Item ||
e.Item.ItemType == System.Web.UI.WebControls.ListItemType.AlternatingItem)
{
try
{
rowVal = Convert.ToDouble(e.Item.Cells[ 3 ].Text);
if (rowVal < 0)
{
e.Item.Cells[ 3 ].ForeColor = Color.Red;
}else{
e.Item.Cells[ 3 ].ForeColor = Color.Black;
}
}

catch(Exception r){
r.ToString();
}
}
}



:

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


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
 
Back
Top