【发布时间】:2017-04-28 10:34:19
【问题描述】:
我对 c# 比较陌生,我正在创建一个 Windows 应用程序,它将读取文本文件中的所有行。用户在DataGridView控件的Column[0]中输入需要替换的字符串,在Column1中输入需要替换的文本。
我创建了两个字符串数组column0 和column1。 但是,在替换行中的字符串时出现错误 (column0, column1)
以下是我的代码:
string[] column0 = new string[dgvMapping.Rows.Count];
string[] column1 = new string[dgvMapping.Rows.Count];
int j = 0;
foreach(DataGridViewRow row in dgvMapping.Rows)
{
if (!string.IsNullOrEmpty(Convert.ToString(row.Cells[0].Value)))
{
column0[j] = Convert.ToString(row.Cells[0].Value);
column1[j] = Convert.ToString(row.Cells[1].Value);
j++;
}
}
var _data = string.Empty;
String[] arrayofLine = File.ReadAllLines(ofd.FileName);
using (StreamWriter sw = new StreamWriter(ofd.FileName + ".output"))
{
for (int i = 0; i < arrayofLine.Length; i++)
{
string line = arrayofLine[i];
line = line.Replace(column0[i], column1[i]);
sw.WriteLine(line);
}
}
我正在使用 OpenFileDialog 来选择文件。
【问题讨论】:
-
“我遇到了一个错误” - 如果你能说明实际的错误是什么,这会有所帮助。
-
@stuartd 我已经更新了错误的帖子
-
现在使用调试器找出哪一行引发了异常
-
@stuartd 这一行:line = line.Replace(column0[i], column1[i]);
-
所以看起来您的文件中的行数比 DGV 中的行数多
标签: c# winforms datagridview