简体   繁体   English

没有从 dataGridView 获取的最后输入值

[英]There is no last entered value taken from dataGridView

FileStream fs = new FileStream(@"file.txt", FileMode.Create);
StreamWriter streamwriter = new StreamWriter(fs);

try
{
    for (int j = 0; j < dataGridView1.Rows.Count -1 ; j++)
    {
        for (i = 0; i < dataGridView1.Rows[j].Cells.Count; i++)
        {
            streamwriter.Write(dataGridView1.Rows[j].Cells[i].Value + "#");
        }

        streamwriter.WriteLine("\n");
    }

    streamwriter.Close();
    fs.Close();
    MessageBox.Show("File saved successfully!");
}
catch
{
    MessageBox.Show("File save failed!");
}

This function should transfer the values from the DataGridView to the file but the last value is not constantly written.这个 function 应该将值从DataGridView传输到文件,但不会不断写入最后一个值。 I realized that it deletes the last element I entered我意识到它删除了我输入的最后一个元素

It should be like this:它应该是这样的:

Name1#Lastname1#Number1#Home1#

This is real result:这是真实的结果:

Name2#Lastname2#Number2##

The problem is in your loop iterating over rows, right now your condition is问题出在你的循环遍历行中,现在你的情况是

j < dataGridView1.Rows.Count -1

This means if the row count is 10, your loop will go through indexes 0 to 8, and skip index 9.这意味着如果行数为 10,您的循环将 go 通过索引 0 到 8,并跳过索引 9。

You should change your condition to你应该改变你的条件

j < dataGridView1.Rows.Count

The posted code will work “IF” the DataGridView's property AllowUsersToAddRows is set to true .发布的代码将在“如果” DataGridView's属性AllowUsersToAddRows设置为true时起作用。 If it is set to false , then, the posted code is going to miss the last row in the grid.如果它设置为false ,那么发布的代码将错过网格中的最后一行。

In addition, depending on the grids DataSource , the data source itself may not allow new rows or be structured to add new rows.此外,根据网格DataSource ,数据源本身可能不允许新行或被构造为添加新行。 In this case, the AllowUsersToAddRows property becomes irrelevant, however in either case… the posted code will still miss the last row.在这种情况下, AllowUsersToAddRows属性变得无关紧要,但是在任何一种情况下……发布的代码仍然会错过最后一行。

Since the code is technically correct “IF” the grid's AllowUsersToAddRows property is set to true .由于代码在技术上是正确的“IF”,因此网格的AllowUsersToAddRows属性设置为true And, your question appears to be about the “missing” last row… I would assume that either the grids AllowUsersToAddRows property is set to false or the data source does not allow adding new rows.而且,您的问题似乎是关于“丢失”的最后一行……我假设网格AllowUsersToAddRows属性设置为false或数据源不允许添加新行。

To avoid this issue… I suggest an extra check for this “new” row in the row loop.为了避免这个问题……我建议在行循环中对这个“新”行进行额外检查。 If you check for the “new” row in the row loop, then it will not matter if the grids AllowUsersToAddRows property is true or false .如果您在行循环中检查“新”行,那么网格的AllowUsersToAddRows属性是true还是false都无关紧要。 Also using a foreach loop through the rows and cells may make things easier and may look something like…此外,在行和单元格中使用foreach循环可能会使事情变得更容易,并且可能看起来像……

foreach (DataGridViewRow row in dataGridView1.Rows) {
  if (!row.IsNewRow) {
    foreach (DataGridViewCell cell in row.Cells) {
      streamwriter.Write(cell.Value + "#");
    }
    streamwriter.WriteLine("");
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM