473,396 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 7975
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mike MacSween | last post by:
This looks like a bug to me. I have an expression on a report: =Format(Sum((**)*(/)),"0.00") Score is byte PercentOfGrade is double PropDegree is single ModuleCats is byte
1
by: J.B | last post by:
I have a datagrid that will display different datasets, (it runs different sprocs based on a value in the querystring) but it will always return 6 columns. The 5th and 6th columns are always Date...
2
by: Wayne Wengert | last post by:
I want to format a date field in a DataGrid column as mm/dd/yyyy. I am using the following code: <asp:DataGrid id="DataGrid1" AutoGenerateColumns="false" EnableViewState="False" runat="server">...
1
by: Julius Fenata | last post by:
Dear all, I need help to change my item-template value format... Here is my case, I have a datagrid, with 'SubjectPrice' field, and when the grid displayed, my 'SubjectPrice' field displayed...
2
by: | last post by:
Hi all, I need to format screen output for a textbox that's bound to a datasource. I know how to format the data in a plain old string, but in this case I'm not sure where to "sneak" in there...
6
by: John A Grandy | last post by:
i thought the following would work for currency {0:$000,000,000.00} but it is displaying ( for example ) : $000,025,368.87 i don't want the extra zeros ...
6
by: KevinW | last post by:
I need to format a currency column in a datagrid. The line I have tried is: Me.DataGrid1.TableStyles(0).GridColumnStyles(3).ToString.Format("#,##0.00") It does not work. I can format the...
3
by: Phil | last post by:
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...
3
by: Mike L | last post by:
Data in datagrid shows all numbers to the 1000th place. So, $3.00 is shown 3.0000 How do I set the datagrid column to currency format? DataGridColumnStyle colStyle4 = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.