ComboBox包含相同的数据

时间:2018-01-16 15:23:18

标签: c# xml winforms combobox

我将XML中的数据添加到ComboBox,但我有一些重复的数据 我该如何解决这个问题?

我尝试了以下内容:

var itemTar = from key in xmlDocument.Descendants("donem")
                        select key.Value;
        //comboBox2.DataSource = itemTar.ToList();
        if (comboBox2.Contains(itemTar.ToString()))
        {

        }

        for(int i = 0; i < comboBox2.Items.Count; i++)
        {
            for(int j=0; j < comboBox2.Items.Count; j++)
            {
                if (comboBox2.Items[i] == comboBox2.Items[j])
                {
                    comboBox2.Items.Remove(comboBox2.Items[j]);
                }
                else
                {
                    continue;
                }
            }
        }

这是我的xml文件:

<?xml version="1.0" encoding="ISO-8859-9"?>
<all>
<s_section id="119342">
<ad>a tic.</ad>
<donem id="10890686" dt="31/12/2017" ds="01/01/2017" s="1">01/01/2017 - 
31/12/2017</donem>
</s_section>
<s_section id="4248622">
<ad>DENEME</ad>
<donem id="9851537" dt="31/12/2017" ds="01/01/2017" s="1">01/01/2017 -             
31/12/2017</donem>
</s_section>
<s_section id="4678788">
<ad>DENETİM16</ad>
<donem id="10711887" dt="31/12/2017" ds="01/01/2017" s="1">01/01/2017 - 
31/12/2017</donem>
<donem id="10711886" dt="31/12/2016" ds="01/01/2016" s="1">01/01/2016 - 
31/12/2016</donem>
</s_section>

1 个答案:

答案 0 :(得分:1)

如果您只需要删除重复的字符串,您只需要调用Distinct然后设置组合框的DataSource。

var itemTar = (from key in xmlDocument.Descendants("donem")
                    select key.Value).Distinct().ToList();
comboBox2.DataSource = itemTar;