Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

format listview column from numeric to int

Tags:

c#

winforms

I have a listview populated by this code :

if (popula.Rows.Count > 0)
{
     foreach (DataRow row in popula.Rows)
     {
         listView1.Items.Add(row[0].ToString());
         listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[1].ToString());
         listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[2].ToString());
         listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[3].ToString());
         listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[4].ToString());
     }

The first column is numeric with this format :

  Code
 ------------- 
  69675.0000
   6078.0000
  57536.0000
  37625.0000
  54079.0000
  57933.0000
  51968.0000
  59160.0000
  25392.0000

I want o display only :

69675
 6078   

i dont like to show the zeros and the point. maybe sonthing like this

 listView1.Items[listView1.Items.Count - 1].SubItems.Add.format ((row[4].ToString()));

Blam i have write

 listView1.Items.Add(row[0].ToString("F0"));

and show me this error : 'No overload for method 'Testing' take 1 argument.

I have add

 using system.globalization; 

but still show me error;

like image 823
alejandro carnero Avatar asked Jun 16 '26 03:06

alejandro carnero


1 Answers

When you do number.ToString() on double or floating point numbers it defaults to a certain number of decimal places.

You can change this by adding a format string parameter to the ToString call like so:

((double)row[4]).ToString("F0")

F reffers to a Fixed-point format, and the number after refers to how many digits it will display after the point. So F0 is a fixed point number with no digits after the point, basically an Integer.

More info: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#FFormatString

like image 154
Blam Avatar answered Jun 17 '26 17:06

Blam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!