Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Formatting a negative number in datagrid in asp.net

Posted on 2004-10-06
7
Medium Priority
?
648 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 500 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 500 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

618 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