简体   繁体   English

在数据网格视图中读取数字的开头0-C#.NET

[英]Reading initial 0 of number in data grid view - C#.NET

I am trying to read data from the text file. 我正在尝试从文本文件读取数据。 My text file consist of data, say, 002200;20. 我的文本文件包含数据,例如002200; 20。

I am displaying those value in data grid view. 我在数据网格视图中显示这些值。 Every time, I am displaying data from text file it reads only 2200;20. 每次,我显示文本文件中的数据时,它的读数仅为2200; 20。 It doesn't read initial 0 any time. 它不会随时读取初始0。

I want to read 002200 instead of 2200. 我想读002200而不是2200。

I am using double as a data type for that. 我正在使用double作为数据类型。

Can anyone help me for that? 有人可以帮我吗?

Thanks, Rushabh Shah. 谢谢,鲁莎莎(Roshabh Shah)。

Numbers do not have leading zeroes. 数字没有前导零。

If you want to always have leading zeroes, you should set the grid to use a format string . 如果要始终使用前导零,则应将网格设置为使用格式字符串

If you want to preserve leading zeroes from the file, you should read strings, not numbers. 如果要保留文件中的前导零,则应读取字符串,而不是数字。

If all you are doing is displaying a value straight from a text file, declare the variable as a string instead of a double. 如果您只是在显示文本文件中的值,则将变量声明为字符串而不是双精度型。 Then it will display as-is in the Grid View. 然后它将按原样显示在“网格视图”中。

You have two approaches you can take with this: 您可以采用两种方法:

  1. You can read the data in as a string value, and then convert it to a double whenever you need to do calculations with it. 您可以将数据作为字符串值读取,然后在需要进行计算时将其转换为双精度型。

  2. You can continue to read the data in as a double, but when you go to display it, you can use the following code: 您可以继续以双重方式读取数据,但是当您显示它时,可以使用以下代码:

    MyDoubleValue.ToString().PadLeft(6, "0"c); MyDoubleValue.ToString()。PadLeft(6,“ 0” c);

This will put 0 characters to the left of it until you reach 6 characters total in length. 这将在其左侧放置0个字符,直到总长度达到6个字符。 You can adjust this length based on your input value. 您可以根据输入值调整此长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM