472,950 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,950 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 7955
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.