如何修改列表视图列
本文关键字:列表 视图 修改 何修改 | 更新日期: 2023-09-27 18:04:09
我有列表视图,数据将从数据表显示在列表视图中我已经这样做了,但是我在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();
}
我的问题是我从数据库得到的原始值是2500000 at dr[6]
我的意思是在这一行newitem.SubItems.Add(dr[6].ToString());
但是我必须像这样只显示两个小数点后25.00
有人能帮我吗?
使用
dr[6].ToString("N2")
更新:((double)dr[6]).ToString("N2")
必须在数字类型上进行N2操作,因此必须对DataRow对象进行强制类型转换。
try this:
string r = "1000.123456";
var t = string.Format("{0:#.##}",decimal.Parse(r)); //1000.12
作为Jason Down给出正确答案。Try dr[6].ToString("0.00") also
并尝试阅读标准数字格式字符串和自定义数字格式字符串以获取有关格式的更多信息。
快乐编码。