gpt4 book ai didi

c# - 如何读取文本文件然后使用分隔线放置数据?

转载 作者:行者123 更新时间:2023-11-30 13:19:26 24 4
gpt4 key购买 nike

private void open(object sender, EventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog();

if (openDialog.ShowDialog() == DialogResult.OK)
{

string[] lines = File.ReadAllLines(openDialog.FileName);

foreach (string line in lines)
{
var text = line.Split(',', '\n');
dataGridView1.Rows.Add(text);
}

}
}

所以在这段代码中,我可以读取一个文本文件,但我只能像它在 1 个网格上一样进行操作。示例文本文件如下所示:

test,1,2,3,4,5,6,
testing,7,8,9,10,11,12,
working,13,14,15,16,17,18,
multiline,19,20,21,22,23,24,
NEXTDATA
see,25,26,36,46,56,66,
program,7,8,9,10,11,12,
macroer,13,14,15,16,17,18,
readextra,19,20,21,22,23,24,

因此,如果我到达名为“NEXTDATA”的行,我希望将该数据放入 dataGridView2。我如何检查它是否到达该行,然后它会执行与 dataGridView1 中的代码相同的操作?

我还使用 ReadLine()ReadAllLines() 吗?

最佳答案

尝试使用这样的东西:

var targetGridView = dataGridView1;
foreach (string line in lines)
{
if (line == "NEXTDATA")
{
targetGridView = dataGridView2;
continue;
}

var text = line.Split(',', '\n');
targetGridView.Rows.Add(text);
}

或者如果您需要在 dataGridView1dataGridView2 之间来回切换,试试这个:

var targetGridView = dataGridView1;
foreach (string line in lines)
{
if (line == "NEXTDATA")
{
targetGridView = (targetGridView == dataGridView1)
? dataGridView2 : dataGridView1;
continue;
}

var text = line.Split(',', '\n');
targetGridView.Rows.Add(text);
}

关于c# - 如何读取文本文件然后使用分隔线放置数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21295965/

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