如何添加DataTable中列前导零 [英] How to add leading zeros in DataTable columns

查看:180
本文介绍了如何添加DataTable中列前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加个零数据表中的列值的面前,如果lenght低于6位数。

I'm trying to add zeros in front of datatable column values if the lenght is less than 6 digits.

例如:我在数据表中与数值123,2345,56,34523等有一栏SRNumber,并希望有我的结果是这样000123,002345,000056,034523

Ex: I have a column "SRNumber" in datatable with values 123, 2345, 56, 34523 etc and Would like to have my results like this 000123, 002345, 000056, 034523.

请注意:我想在DataTable中的操作没有在SQL查询以检索数据的数据。我见过的解决方案,我们可以添加前导零到字符串或变量,但在mycase想这样做对数据表中的列中的所有值。

Note: I would like the manipulate the data in Datatable not in the sql query which retrieves the data. I have seen the solutions where we can add leading zeros to a string or variable but in mycase would like to do it for all the values in datatable columns.

推荐答案

好吧,我@eddie_cat答案帮助了它。这里是我做到了。

ok, i figured it out with help of @eddie_cat answers. Here is what i did.

foreach (DataRow row in table.Rows)
{
      row["SRNumber"] = row["SRNumber"].ToString().PadLeft(6, '0');
}
table.AcceptChanges();

在这里,SRNumber是在我加入前导零的列名。我希望这可以帮助别人。

Here, "SRNumber" is the column name in which i'm adding leading zero's. I hope this helps someone.

这篇关于如何添加DataTable中列前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆