Format a money value ListView Subitem as decimal and allow for nulls

I have a listview that I am populating from a data table.  The database field "Credit_Limit" is "money" type, and can contain null values.
The code below works -- nulls show nicely as an empty cell in the ListView, BUT "Credit_Limit" is shown like "500.0000" and I want it formated like "500.00"

What's the simplest way to add the ListView.SubItem formatted the way I want, an account for nulls?
Thanks!

foreach (DataRow row in myTable.Rows)
    {
        ListViewItem myListItem = new ListViewItem(row["Customer_Id"].ToString(), 1);
        myListItem.SubItems.Add(row["Cust_Name"].ToString());
        myListItem.SubItems.Add(row["Credit_Limit"].ToString());
        lstCustomers.Items.Add(myListItem);
     }

Open in new window

Rob RudloffIT Development SpecialistAsked:
Who is Participating?
 
Rob RudloffIT Development SpecialistAuthor Commented:
OK, it seems it can't be done in one step ... so --

To display nulls as empty cell:
     double DD;
     string SS;
     SS = (Double.TryParse(row["Credit_Limit"].ToString(), out DD) ? DD.ToString("0.00") : "");
     myListItem.SubItems.Add(SS);

Open in new window


To diplay nulls as 0:
     double DD;
     Double.TryParse(row["Credit_Limit"].ToString(), out DD);
     myListItem.SubItems.Add(DD.ToString("0.00"));

Open in new window


Just my best guess -- what is the "normal" way of doing this?
-- thanks
0
 
Luis PérezSoftware Architect in .NetCommented:
Try this:

myListItem.SubItems.Add(row["Credit_Limit"].ToString("#0.00"));

Hope that helps.
0
 
Rob RudloffIT Development SpecialistAuthor Commented:
Actually I originally tried that, since that's how I understood "ToString" to work ... but, I get the error:
"No overload for method 'ToString' takes 1 arguments"

0
 
Rob RudloffIT Development SpecialistAuthor Commented:
this solution is the only one tht works
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.