gpt4 book ai didi

c# - while循环用数组填充datagridview

转载 作者:太空宇宙 更新时间:2023-11-03 13:10:50 25 4
gpt4 key购买 nike

我有一组数据,一旦它到达文本文件中的特定点,我就需要用这些数据填充数据 GridView 。

一旦 (StreamReader reader = new StreamReader(fileOpen)) 到达文件中的 [HRData],我想将每一列存储到一个数组中,以便存储到 datagridview 中并循环到文件

[HRData]
91 154 70 309 83 6451
91 154 70 309 83 6451
92 160 75 309 87 5687
94 173 80 309 87 5687
96 187 87 309 95 4662
100 190 93 309 123 4407
101 192 97 309 141 4915
103 191 98 309 145 5429
106 190 99 309 157 4662
106 187 98 309 166 4399
107 186 97 309 171 4143
107 185 97 309 170 4914
108 184 96 309 171 5426
108 183 95 309 170 5688

最佳答案

您可以使用 LINQ 生成数组列表(每个元素是一行)(每个元素是该行中的数字)

List<string[]> result = File.ReadAllLines("filePath") //read all lines in the file
.SkipWhile(x => x != "[HRData]") //skip lines until reaching the "[HRData]"
.Select(x => x.Split(null, StringSplitOptions.RemoveEmptyEntries)) //split line by whitespace to get numbers in line
.ToList(); // convert the result to a list

然后您可以使用 result.ForEach(x => dataGridView1.Rows.Add(x))

关于c# - while循环用数组填充datagridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28766768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com