从组合框列表中删除重复项

编程入门 行业动态 更新时间:2024-10-08 08:32:14
本文介绍了从组合框列表中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我用数据库中的数据填充了组合框,我想从组合框项目列表中删除重复的名称。你能给我一个代码吗?

I have populated the combo box with data from a database and I wanted to remove the duplicate names from the combo box items list. Can you give me a code to do it?

推荐答案

它不一定那么简单 - 它取决于你填充ComboBox的项目类型用。因为你不能只设置Items数组(它不公开公共setter,只是getter)你必须创建一个新的项目数组,删除重复项,然后将它设置回ComboBox: It's not necessarily as simple as that - it depends to an extent on what type of items you have filled your ComboBox with. Because you can't just set the Items array (it exposes no public setter, just the getter) you have to create a new array of items, remove the duplicates, and set it back into the ComboBox: List<object> list = new List<object>(); foreach (object o in myComboBox.Items) { if (!list.Contains(o)) { list.Add(o); } } myComboBox.Items.Clear(); myComboBox.Items.AddRange(list.ToArray());

为什么不从数据库本身获取唯一的? 喜欢: Why not just fetch the unique ones from database itself? Like: SELECT DISTINCT Names FROM MstNames

如果您仍然希望在分配数据源之前过滤要使用的数据表。 要了解更多信息,请阅读: MSDN :DataView.RowFilter属性 [ ^ ]

由于Sandeep的答案最佳方式是从数据库中选择时选择不同。 如果你使用c#3.0或更高版本你可以尝试下面的代码 As Sandeep's answer best way is select distinct when you select from database. if you are using c# 3.0 or later you can try below code var itemArry= myComboBox.Items.SelectMany(i => i).Distinct().ToArray(); myComboBox.Items.Clear(); myComboBox.Items.AddRange(itemArry);

更多推荐

从组合框列表中删除重复项

本文发布于:2023-11-30 12:37:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649988.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   列表中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!