C# 如何修改列表视图列?
我有列表视图数据将显示在数据表的列表视图中 像这样,我已经做了,但我在数据行6有问题C# 如何修改列表视图列?,c#,listview,decimal,datarow,C#,Listview,Decimal,Datarow,我有列表视图数据将显示在数据表的列表视图中 像这样,我已经做了,但我在数据行6有问题 dt = classes.xxxxx.GetData(sql, mf); if (dt != null) { ListViewItem newitem = null; lstviewcashmembers.Items.Clear(); lstviewcashmembers.BeginUpdate(); foreach (DataRow dr in
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();
}
我的问题是,我从数据库中得到的原始值是25.00000,位于dr[6]
我的意思是在这一行中,newitem.SubItems.Add(dr[6].ToString())
但我只需要显示两位小数点后25.00
有人能帮忙吗?用这个:
dr[6].ToString("N2")
更新:
((double)dr[6]).ToString("N2")
N2必须对数字类型执行,因此必须对DataRow对象执行强制转换。尝试以下操作:
string r = "1000.123456";
var t = string.Format("{0:#.##}",decimal.Parse(r)); //1000.12
当Jason倒下时,请给出正确答案。也可以尝试dr[6]。ToString(“0.00”) 并尝试阅读和了解有关格式的更多信息
愉快的编码。获取了一个错误To字符串的无重载方法接受一个参数newitem.SubItems.Add(dr[6].ToString(“N2”)@user682417:嗯,也许dr[6]必须先转换为双精度。。。更新回答有价值的问题+1。