3

I have list view the data will be displayed in list view from data table like this i have done but i have problem at datarow 6

           dt = classes.xxxxx.GetData(sql, mf);

  if (dt != null)
  {
    ListViewItem newitem = null;
    lstviewcashmembers.Items.Clear();
    lstviewcashmembers.BeginUpdate();
    foreach (DataRow dr in dt.Rows)
    {
      newitem = lstviewcashmembers.Items.Add(dr[0].ToString());
      newitem.SubItems.Add(dr[1].ToString());
      newitem.SubItems.Add(dr[2].ToString());
      newitem.SubItems.Add(dr[3].ToString());
      newitem.SubItems.Add(dr[4].ToString());
      newitem.SubItems.Add(dr[5].ToString());
      newitem.SubItems.Add(dr[6].ToString());
      newitem.SubItems.Add(dr[7].ToString());
      newitem.SubItems.Add(dr[8].ToString());
      newitem.SubItems.Add(dr[9].ToString());
      newitem.SubItems.Add(dr[10].ToString());
      newitem = null;
    }

    lstviewcashmembers.EndUpdate();
  }

my problem is like I got original value coming from database is 25.00000 at dr[6]

I mean in this line newitem.SubItems.Add(dr[6].ToString());

But I have to show only two decimal places like this 25.00

Would any one help this?

Sagotharan
  • 2,586
  • 16
  • 73
  • 117
user682417
  • 1,478
  • 4
  • 25
  • 46

3 Answers3

5

Use this:

dr[6].ToString("N2")

Update:

((double)dr[6]).ToString("N2")

The N2 must be done on a numeric type, so the cast is necessary on the DataRow object.

Jason Down
  • 21,731
  • 12
  • 83
  • 117
1

try this:

 string r = "1000.123456";
 var t = string.Format("{0:#.##}",decimal.Parse(r)); //1000.12
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
0

As Jason Down give the Correct Answer. Try dr[6].ToString("0.00") also

And try to read Standard Numeric Format Strings and Custom Numeric Format Strings For More Information About Format.

Happy Coding.

Sagotharan
  • 2,586
  • 16
  • 73
  • 117