这种格式有什么问题? [英] What is wrong with this formatting?

查看:74
本文介绍了这种格式有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看不出有什么问题.

  foreach ( dataSet.Tables [字符串 tmp = 字符串 .Format("  {0}",行[  PZN" span>].ToString().Trim().PadLeft( 7 ' " ] = tmp;
    Console.WriteLine(tmp);
    Console.WriteLine(row [" ]);;
} 



给我类似...的输出

0930673<br />
930673<br />
0148814<br />
148814



在打印行时,请注意缺少的零.尝试dataSet.Tables[0].AcceptChanges();无效. DataSet来自Excel文件,使用Microsoft.ACE.OLEDB.12约有110个结果,并在DataGridView中可视化.除了重新分配/重新设置行的格式外,其他所有程序都工作正常.

恕我直言,这应该可以工作.

谢谢,伙计们.

解决方案

我的猜测是您的row ["PZN"]拥有一个Integer.您的字符串已成功解析为整数,并且不会引发异常,但是转换前的0丢失了;)


这可能有点难以解释,但是row [" PZN]是一个对象-并非源自该对象的任何东西.这也是一个相当特殊的对象,因为当您分配给它时,它会转换为DataTable的基础类型-在这种情况下,我怀疑是int的名称.因此,当您读回它并使用ToString对其进行读取时,将再次使用基础数据类型,并且完成了标准的从int到字符串的转换.
自己尝试:

  int  i =  0 ;
 foreach (数据行 in  dt.Rows中的行)
    {
    字符串 s = i.ToString().PadLeft( 7 '  0');
    i ++;
    对象 o = s;
    row [" ] = s;
    Console.WriteLine(" ,s,o. ToString(),row [" ]));
    row [" ] = s;
    Console.WriteLine(" ,s,o. ToString(),row [" ]));
    } 


结果是:

0000000:0000000:0
0000000:0000000:0000000
0000001:0000001:1
0000001:0000001:0000001
0000002:0000002:2
0000002:0000002:0000002


也许(这只是一种猜测),因为tmp被显式声明为字符串,而row ["PZN"]不是?

I do not see what''s wrong.

foreach (DataRow row in dataSet.Tables[0].Rows)
{
    string tmp = String.Format("{0}", row["PZN"].ToString().Trim().PadLeft(7, '0'));
    row["PZN"] = tmp;
    Console.WriteLine(tmp);
    Console.WriteLine(row["PZN"]);
}



gives me output like...

0930673<br />
930673<br />
0148814<br />
148814



Note the missing zeros while printing the row. tried dataSet.Tables[0].AcceptChanges(); no effect. DataSet comes from Excel-File using Microsoft.ACE.OLEDB.12 with about 110 results and is visualized in DataGridView. Everything is working fine except the reassignment/reformatting of the rows.

IMHO this is suppose to work.

Thanks, guys.

解决方案

My guess is that your row["PZN"] holds an Integer. Your String is successfully parsed as an Integer and will not throw an Exception, but the first 0 gets lost in the conversion ;)


This is probably a little difficult to explain, but row["PZN"] is an object - not anything derived from an object. It is also a fairly special object in that when you assign to it, it gets converted to the underlying type for the DataTable - in this case, int, I suspect from the name. So, when you read it back and ToString it, the underlying datatype is used again, and a standard int-to-string convertion is done.
Try it yourself:

int i = 0;
foreach (DataRow row in dt.Rows)
    {
    string s = i.ToString().PadLeft(7, '0');
    i++;
    object o = s;
    row["myInt"] = s;
    Console.WriteLine("{0}:{1}:{2}", s, o.ToString(), row["myInt"]);
    row["myStr"] = s;
    Console.WriteLine("{0}:{1}:{2}", s, o.ToString(), row["myStr"]);
    }


The results are:

0000000:0000000:0
0000000:0000000:0000000
0000001:0000001:1
0000001:0000001:0000001
0000002:0000002:2
0000002:0000002:0000002


Perhaps (and this is just kind of a guess) its because tmp is declared explicitly as a string, and row["PZN"] isn''t?


这篇关于这种格式有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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