c#从文件中读取行并替换为DataGridView数据中的文本

c#从文件中读取行并替换为DataGridView数据中的文本,c#,winforms,datagridview,C#,Winforms,Datagridview,我对c#比较陌生,我正在创建一个windows应用程序,它可以读取文本文件中的所有行。用户将在[0]列中输入需要替换的字符串,并在DataGridView控件的列中输入需要替换的文本 我已经创建了两个字符串数组column0和column1。 但是,替换第行(第0列,第1列)中的字符串时出错 以下是我的代码: string[] column0 = new string[dgvMapping.Rows.Count]; string[] column1 = new s

我对c#比较陌生,我正在创建一个windows应用程序,它可以读取文本文件中的所有行。用户将在[0]列中输入需要替换的字符串,并在DataGridView控件的列中输入需要替换的文本

我已经创建了两个字符串数组column0和column1。 但是,替换第行(第0列,第1列)中的字符串时出错

以下是我的代码:

        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);
            }
        }
string[]column0=新字符串[dgvMapping.Rows.Count];
string[]column1=新字符串[dgvMapping.Rows.Count];
int j=0;
foreach(dgvMapping.Rows中的DataGridViewRow行)
{
如果(!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);
使用(StreamWriter sw=新StreamWriter(ofd.FileName+“.output”))
{
for(int i=0;i
我正在使用OpenFileDialog选择文件

执行时出错:
Stuartd是正确的…文件中的行数多于要搜索的元素数。从某种意义上说,我不确定搜索在做什么,因为它似乎有点有限。代码会根据项目所在的行搜索每个项目。第0列中的搜索值和第0行第1列中的替换值…将仅替换文件中第一行的那些值。
DataGridView
s的第二行值将只搜索/替换第二行,依此类推。这似乎很奇怪

示例两个字符串数组(column0和column1)的大小设置为
dgvMapping
中的行数。假设网格中有5行,那么数组大小将是5个字符串。当启动循环以写入字符串时,循环从0开始,并在文件中的行数处停止。代码使用此
i
变量作为两个数组的索引。如果文件中的行数多于网格中的行数…则会出现错误

同样,以这种方式进行搜索和替换似乎有些奇怪。假设要在列0的所有行中搜索每个术语,并将找到的搜索字符串替换为列1中的替换字符串,则需要在网格的每一行中循环文件中的每一行。这将用文件中的所有行替换网格中的所有搜索/替换项。如果这就是你要完成的,下面是实现这一点的一种方法,然而…可能有更好的方法来实现这一点

下面的代码将文件读入一个大字符串。然后,代码在所有网格行中循环,以搜索/替换大字符串中的字符串。希望这有帮助

 string bigString = File.ReadAllText(ofd.FileName);
  try {
    using (StreamWriter sw = new StreamWriter(ofd.FileName + ".output")) {
      for (int k = 0; k < dgvMapping.Rows.Count; k++) {
        if (dgvMapping.Rows[k].Cells[0].Value != null && dgvMapping.Rows[k].Cells[1].Value != null) {
          string searchTerm = dgvMapping.Rows[k].Cells[0].Value.ToString();
          string replaceTerm = dgvMapping.Rows[k].Cells[1].Value.ToString();
          if (searchTerm != "") {
            bigString = bigString.Replace(searchTerm, replaceTerm);
          } else {
            // one of the terms is empty
          }
        } else {
          // one of the terms is null}
        }
      }
      sw.WriteLine(bigString);
    }
  }
  catch (Exception ex) {
    MessageBox.Show("Write Erro: " + ex.Message);
  }
string bigString=File.ReadAllText(ofd.FileName);
试一试{
使用(StreamWriter sw=新StreamWriter(ofd.FileName+“.output”)){
对于(int k=0;k
您正在围绕一个行数未知的文件循环,并假设网格中的行数与文件中的行数完全相同。只有当文件和gridView的行数相同时,您的代码才会工作

解决方案之一是在线阵列上循环(正如您已经做的那样),并搜索当前线在DGV中包含关键点的GridViewRow。如果是这种情况,则用该行中的值(从gridView获得)替换所有出现的键,否则不执行任何操作

查看下面的代码:

// Convert the row collection to a list, so that we could query it easily with Linq
List<DataGridViewRow> mySearchList = dataGridView1.Rows.Cast<DataGridViewRow>().ToList();
const int KEY_INDEX = 0; // Search index in the grid
const int VALUE_INDEX = 1; // Value (replace) index in the grid
for (int i = 0; i < arrayofLines.Length; i++)
{

    string line = arrayofLines[i];

    // Get data grid view Row where this line contains the key string
    DataGridViewRow matchedRow = mySearchList.FirstOrDefault(obj => line.Contains(obj.Cells[KEY_INDEX].Value.ToString()));
    // If this row exists, replace the key with the value (obtained from the grid)
    if (matchedRow != null)
    {
        string key = matchedRow.Cells[KEY_INDEX].Value.ToString();
        string value = matchedRow.Cells[VALUE_INDEX].Value.ToString();

        line = line.Replace(key, value);

        sw.WriteLine(line);
    }
    else
    {
        // Otherwise, do nothing
    }
}
//将行集合转换为列表,以便使用Linq轻松查询
List mySearchList=dataGridView1.Rows.Cast().ToList();
const int KEY_INDEX=0;//网格中的搜索索引
常量int值_索引=1;//网格中的值(替换)索引
for(int i=0;iline.Contains(obj.Cells[KEY\u INDEX].Value.ToString());
//如果此行存在,请将键替换为值(从网格中获取)
if(matchedRow!=null)
{
字符串key=matchedRow.Cells[key_INDEX].Value.ToString();
string value=matchedRow.Cells[value_INDEX].value.ToString();
行=行。替换(键、值);
西南写入线(行);
}
其他的
{
//否则,什么也不做
}
}

“我遇到了一个错误”-如果你能告诉我实际的错误是什么,这会很有帮助。@stuartd我已经用error更新了帖子现在使用调试器找出哪一行引发了错误exception@stuartd此行:Line=Line.Replace(第0列[i],第1列[i]);因此,看起来文件中的行数比DGV中的行数多