添加数据网格项时将字符串转换为 int32
伙计们,很抱歉问了这个基本问题。我试图找出答案,但我仍然被它困住了。我是编程新手,所以如果你们能帮助我,那就太有意义了。谢谢。 对于这段代码,我得到了该网站某人的帮助。
这是我的问题,我有这样的 .txt 数据
<前><代码>10192 20351 30473 40499 50449 60234 10192 20207 30206 40203 50205 60226 10192 20252 30312 40376 50334 60252
这是我的代码,我在其中打开文本文件并解析为数字并将其放入 6 列数据网格中。
所以我的数据在数据网格中是这样的:
第 1 列|第 2 列|第 3 列|第 4 列|第 5 列|第 6 列
10192 | 20351| 30473 | 30473 40499 | 40499 50449 | 50449 60234
private void Parsing_String(string filename)
{
List<Row> list = new List<Row>();
foreach (String str in File.ReadLines(filename))
{
String[] strCols = str.Split(Convert.ToChar(" "));
list.Add(new Row()
{
Column1 = strCols[0].Substring(2),
Column2 = strCols[1].Substring(2),
Column3 = strCols[2].Substring(2),
Column4 = strCols[3].Substring(2),
Column5 = strCols[4].Substring(2),
Column6 = strCols[5].Substring(2),
});
}
dg.ItemsSource = list;
}
public class Row
{
public string Column1 { get; set; }
public string Column2 { get; set; }
public string Column3 { get; set; }
public string Column4 { get; set; }
public string Column5 { get; set; }
public string Column6 { get; set; }
}
我想要做的是,我需要按顺序将数字逐行放入一些数学方程中。我需要将字符串转换为int。但我不知道怎么办,我尝试了一些但失败了。
例如: 首先,我将第一行行放入数据网格中,其中包含 6 组数字并计算它。接下来是第二排。等等。我怎样才能得到我想要的数字并将其放入数学方程中。
无论如何,谢谢。
Guys, sorry for the basic question. I try to find out but I still get stuck with it. I am new to programming so, if you guys can help me, it's mean a lot. Thanks.
For this code to i got help from someone in this site.
Here's my problem,I have a .txt data like this
10192 20351 30473 40499 50449 60234 10192 20207 30206 40203 50205 60226 10192 20252 30312 40376 50334 60252
And this is my code, where I open the text file and parsing into numbers and put it to 6 columns datagrid.
So my data turns out like this in datagrid:
Column 1|Column 2|Column 3|Column 4|Column 5|Column 6
10192 | 20351| 30473 | 40499 | 50449 | 60234
private void Parsing_String(string filename)
{
List<Row> list = new List<Row>();
foreach (String str in File.ReadLines(filename))
{
String[] strCols = str.Split(Convert.ToChar(" "));
list.Add(new Row()
{
Column1 = strCols[0].Substring(2),
Column2 = strCols[1].Substring(2),
Column3 = strCols[2].Substring(2),
Column4 = strCols[3].Substring(2),
Column5 = strCols[4].Substring(2),
Column6 = strCols[5].Substring(2),
});
}
dg.ItemsSource = list;
}
public class Row
{
public string Column1 { get; set; }
public string Column2 { get; set; }
public string Column3 { get; set; }
public string Column4 { get; set; }
public string Column5 { get; set; }
public string Column6 { get; set; }
}
What I want to do is, I need the numbers sequentially row by row to put in some mathematics equation. And I need to convert the string into int. But I don't know how, I put some trial but failed.
for example:
first I put first line of rows in datagrid which contain 6 group of number and calculate it. Next, the second row. and so on. How can I get the number that i want and put it in mathematics equation.
Thanks Anyway.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用数据进行绑定,那么将转换后的值存储到行中应该不会有问题:
无论您在顶部放置什么 UI 内容,它都会对其调用 ToSTring() 并将其显示为字符串。因此,您的 row 对象有 6 个整数。
需要注意的是,在 C# 中,您可以用单引号表示一个 char,然后拆分变为:
str.Split(' ')
为了更优雅,您可以通过以下方式在 Row 对象中进行行解释:将文件的一行传递到构造函数中并在构造函数中执行拆分。然后您可以将行上的设置器设置为私有。 foreach 循环则变为:
If you use the data for binding, there shouldn't be a problem storing the converted values into the row:
Whatever UI stuff you put on top, it will call ToSTring() on it anyway and display the thing as string. Consequently, your row object has 6 ints.
On a minor note, in C# you can denote a char with single quotes, the split then becomes :
str.Split(' ')
For more elegance, you can have the line interpretation inside your Row object by passing a single line of the file into the constructor and performing the split in the constructor. Then you can make the setters on row private. The foreach loop then becomes:
为了进一步说明 flq 的示例,我不会依赖数据,而是会执行类似的操作,因此如果解析失败你会得到一个 0 - 除非你想捕获异常并以不同的方式处理它。
To take flq's example a little farther, I wouldn't count on the data and would do something like this so if the parse fails you get a 0 - unless you want to catch the exception and handle it differently.
您需要使用 Int32.TryParse 将字符串转换为 int32。
You need to use Int32.TryParse to convert the strings to int32s.