将项目从一个列表移动到另一个列表?

发布于 2024-12-02 09:29:53 字数 2465 浏览 0 评论 0原文

我有两个标记为:choiceDGVuniversalDGV 的 DataGridView,它们绑定到标记为:choiceLineBindingSourceuniversalLineBindingSource 的绑定源。

SortableBindingList<ChoiceLine> theChoiceList = new SortableBindingList<ChoiceLine>();
SortableBindingList<UniversalLine> theUniversalList = new SortableBindingList<UniversalLine>();

然后,我使用迭代的 foreach 循环添加到每个列表:

theChoiceList.Add(new ChoiceLine(item1, item2, item3, item4, item5, item6, item7, 
                                 item8, item9, item10, item11, item12, item13));

theUniversalList.Add(new UniversalList(stuff1, stuff2, stuff3, stuff4, stuff5, stuff6, stuff7, 
                                       stuff8, stuff9, stuff10, stuff11, stuff12, stuff13));

现在我调用: choiceSort();universalSort() ;。它们看起来像这样:

private void choiceSort()
{
    var sortedChoice = theChoiceList.OrderBy(l => l.Speed);
    choiceLineBindingSource.DataSource = sortedChoice;
}

private void universalSort()
{
    var sortedUniversal = theUniversalList.OrderBy(l => l.Type);
    universalLineBindingSource.DataSource = sortedUniversal;
}

编辑:

这是移动项目的代码。

private void toUniversalButton_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in choiceDGV.SelectedRows)
    {
        object[] items = new object[row.Cells.Count];
        for (int i = 0; i < row.cells.Count; i++)
            items[i] = row.Cells[i].Value;

        theUniversalList.Add(new UniversalList(item1, item2, item3, item4, item5, item6, item7
                                               item8, item9, item10, item11, item12, item13));
        universalSort();

        choiceDGV.Rows.Remove(row);
    }
}

我的问题

我还有多个按钮可将项目从一个 DGV 移动到另一个 DGV。因此,单击时,从 choiceDGV 中选择的项目将被删除并添加到 unversalDGV 中。但是,当我使用另一个按钮将项目移回后,从 universalDGV 中删除项目并将其添加到 choiceDGV 时,我得到了重复的项目。

如果继续此过程并且我将项目来回移动 4 次,我将获得该特定项目 4 次,而不仅仅是我想要的一个。

所以我几乎得到了重复的东西。有谁知道发生了什么事以及如何修复它,这样当我来回移动时我只能得到一件物品?

编辑——有关该问题的更多信息

我刚刚将每个项目从一个列表移动到另一个列表,以便其中一个 DGV 明显是空的。现在,当我单击按钮将项目从其他 DGV 移动到明显空的 DGV 时,它会重新填充 DGV 所拥有的内容(在清除之前)加 (+) 项目单击移动按钮时选择的...

I have two DataGridViews labeled: choiceDGV and universalDGV that are bound to binding sources labeled: choiceLineBindingSource and universalLineBindingSource.

SortableBindingList<ChoiceLine> theChoiceList = new SortableBindingList<ChoiceLine>();
SortableBindingList<UniversalLine> theUniversalList = new SortableBindingList<UniversalLine>();

I then add to each list using a foreach loops that iterates this:

theChoiceList.Add(new ChoiceLine(item1, item2, item3, item4, item5, item6, item7, 
                                 item8, item9, item10, item11, item12, item13));

theUniversalList.Add(new UniversalList(stuff1, stuff2, stuff3, stuff4, stuff5, stuff6, stuff7, 
                                       stuff8, stuff9, stuff10, stuff11, stuff12, stuff13));

Now I call: choiceSort(); annd universalSort();. They look like this:

private void choiceSort()
{
    var sortedChoice = theChoiceList.OrderBy(l => l.Speed);
    choiceLineBindingSource.DataSource = sortedChoice;
}

private void universalSort()
{
    var sortedUniversal = theUniversalList.OrderBy(l => l.Type);
    universalLineBindingSource.DataSource = sortedUniversal;
}

EDIT:

Here is the code for the moving of the items.

private void toUniversalButton_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in choiceDGV.SelectedRows)
    {
        object[] items = new object[row.Cells.Count];
        for (int i = 0; i < row.cells.Count; i++)
            items[i] = row.Cells[i].Value;

        theUniversalList.Add(new UniversalList(item1, item2, item3, item4, item5, item6, item7
                                               item8, item9, item10, item11, item12, item13));
        universalSort();

        choiceDGV.Rows.Remove(row);
    }
}

My Problem:

I also have multiple buttons that move the item from one DGV to another. So on the click, the selected item(s) from choiceDGV will be removed and added to unversalDGV. However, when I go to move the item(s) back using the other button that removes the item(s) from universalDGV and adds to choiceDGV I get duplicate items.

If this process is continued and I move the item(s) back-and-fourth 4 times, I will get that particular item(s) 4 times instead of just the one that I wanted.

So pretty much I get duplicates. Does anyone know what is going on and how I can fix it so I only get one item when I move back-and-fourth?

EDIT -- More info on the problem

I just moved every item from one list to the other so that one of the DGV's was completely visibly empty. Now when I click the button to move an item(s) from the other DGV to the visibly empty DGV it repopulates what the DGV had (before it was cleared) plus (+) the item(s) that were selected when the move button was clicked...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尹雨沫 2024-12-09 09:29:53

您似乎从未从 theChoiceList 中删除原始项目,您需要在从 choiceDGV 中删除它的同时执行此操作(choiceDGV.Rows.Remove(row)),否则每次您重新排序时,它都会根据 theChoiceList 进行排序仍然包含原始项目和新项目。

You never seem to remove the original item from theChoiceList, you need to do this at the same time you remove it from choiceDGV (choiceDGV.Rows.Remove(row)) otherwise each time you re-sort it will do it based on theChoiceList which still contains the original item plus the new one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文