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.SubItem开发者_如何学Cs.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?
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.
try this:
string r = "1000.123456";
var t = string.Format("{0:#.##}",decimal.Parse(r)); //1000.12
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.
精彩评论