【问题标题】:Erroneous Data Retrieved From ListView从 ListView 检索到的错误数据
【发布时间】:2009-03-13 15:41:01
【问题描述】:

我的程序逻辑循环遍历存在于两个单独 ListView 中的数据集合时遇到了一些问题。在循环并从 ListView 中提取数据之后,我将所有内容添加到逗号分隔的文本文件 (CLOSEULDCONFIG.TXT) 中。

我第一次执行此逻辑时,一切正常。如果我再次执行此逻辑,我将获得 ListView 中的 2 个副本。每次运行这个逻辑,之前添加的 ListView 项的副本数都会增加 1。

这是不可取的,因为我想将与 ListView 中相同数量的元素添加到我的文本文件中。谁能发现导致这种情况的嵌套 foreach 语句有什么问题?

                        // HAZMAT PACKAGE ERROR LISTVIEW ITEMS               
                        foreach (ListViewItem HazPackErrItems in HazmatPackageErrorListview.Items)
                        {
                            bool first = true;
                            foreach (ListViewItem.ListViewSubItem HazPackErrSub in HazPackErrItems.SubItems)
                            { 
                                // removes the first element of each comma delimited string
                                if (first)
                                    first = false;
                                else
                                    CloseULDSubmitLogDataResponseHazpackerrCloseULDConfig += " " + HazPackErrSub.Text + ",";
                            }
                        } 

                        // HAZMAT WEIGHT AND COUNT COLLECTED LISTVIEW ITEMS
                        foreach (ListViewItem HazWeightAndCountItems in HazmatWeightAndCountListview.Items)
                        {
                            bool first = true;
                            foreach (ListViewItem.ListViewSubItem HazWeightAndCountSub in HazWeightAndCountItems.SubItems)
                            {
                               // removes the first element of each comma delimited string
                                if (first)
                                    first = false;
                                else
                                    CloseULDSubmitLogDataResponseHazWeightAndCountCloseULDConfig += " " + HazWeightAndCountSub.Text + ",";
                            }
                        }

                        using (System.IO.StreamWriter sw = new System.IO.StreamWriter("CLOSEULDCONFIG.TXT", true))
                        {
                            if (!AlreadyExists)
                            {
                                sw.WriteLine(PresetNameConfig +
                                CloseULDSubmitLogDataRequestCloseULDConfig +
                                CloseULDSubmitLogDataResponseCloseULDConfig +
                                CloseULDSubmitLogDataResponseHazpackerrCloseULDConfig +
                                CloseULDSubmitLogDataResponseHazWeightAndCountCloseULDConfig +
                                CloseULDDateTimeConfig);
                            }
                        }

【问题讨论】:

    标签: c# algorithm listview loops foreach


    【解决方案1】:

    如果我没记错的话,您打开的是要追加的文件,而不是覆盖。您是否检查了文件本身以查看数据是否在此处重复?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-30
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      • 2018-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多