471,338 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Format Negative Currency in Datagrid

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
Jul 21 '05 #1
3 7882
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

Jul 21 '05 #2
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.Alternating Item)
{
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" wrote:
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

Jul 21 '05 #3
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.
Phil wrote:
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.Alternating Item)
{
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" wrote:

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

Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by J.B | last post: by
2 posts views Thread by Wayne Wengert | last post: by
1 post views Thread by Julius Fenata | last post: by
6 posts views Thread by John A Grandy | last post: by
6 posts views Thread by KevinW | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.