Solved

Formatting a negative number in datagrid in asp.net

Posted on 2004-10-06
7
639 Views
Last Modified: 2008-03-04
Is it possible to format a negative integer in a column in datagrid to show (1) instead of -1.

Ex:
  Column                           Column(This is what I want)
  -1                                            (1)
 -20                                            (20)

Thanks in advance.
0
Comment
Question by:wilkersons
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 18

Accepted Solution

by:
tusharashah earned 125 total points
ID: 12240247
You can do something like following in your ItemDataBound event of DataGrid:
---------------------------------------------------------------------------------------------------------------------------------------
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
      switch( e.Item.ItemType )
      {
      
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                  string strNumber = e.Item.Cells[2].Text;
                  if( strNumber.IndexOf("-") > -1 )
                                                {
                                                      strNumber = strNumber.Replace("-","");
                                                      strNumber = "(" + strNumber + ")";        
                                                }

                                break;
      }
}
---------------------------------------------------------------------------------------------------------------------------------------                              

-tushar            
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12240271
You just need to replace 2 here with your Column Number

e.Item.Cells[2].Text;
0
 
LVL 10

Assisted Solution

by:jnhorst
jnhorst earned 125 total points
ID: 12240469
Do this in the ItemDataBound event.

If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
     ' replace the (x) with the column index in question.
     ' if you are dealing with integers only, use System.Int32.Parse()
     e.Item.Cells(x).Text =  System.Int32.Parse(e.Item.Cells(x).Text).ToString("#;(#)")

     ' if you are dealing with floats (numbers with decimals), use System.Decimal.Parse()
     e.Item.Cells(x).Text =  System.Decimal.Parse(e.Item.Cells(x).Text).ToString("#.##;(#.##)")
End If

John



0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:jnhorst
ID: 12240501
You can also do this:

e.Item.Cells(x).ForeColor = Color.Red

to show the negative values in red.

John
0
 
LVL 33

Expert Comment

by:raterus
ID: 12240580
There is a more elegant way to do this, than the way tusharashah suggests.  I can't say it's any easier though :-)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemglobalizationnumberformatinfoclassnumbernegativepatterntopic.asp
0
 

Author Comment

by:wilkersons
ID: 12241217
Thanks a lot  everyone for ur suggestions.
I tried
Case ListItemType.Item Or ListItemType.AlternatingItem
                             Dim strNumber As String
                strNumber = e.Item.Cells(6).Text
                If (strNumber.IndexOf("-") > -1) Then

                    strNumber = strNumber.Replace("-", "")
                    strNumber = "(" & strNumber & ")"
                    e.Item.Cells(6).Text = strNumber
                End If

AND I ALSO TRIED
Case ListItemType.Item Or ListItemType.AlternatingItem
                e.Item.Cells(6).Text = System.Int32.Parse(e.Item.Cells(6).Text).ToString("#,##;(#,##)")

BOTH OF THEM WORKED.

Thanks a lot

0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12241295
Nice to have you going!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question