Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从comboBox中消除重复值?_C#_Combobox_Duplicates_Oledbdatareader - Fatal编程技术网

C# 如何从comboBox中消除重复值?

C# 如何从comboBox中消除重复值?,c#,combobox,duplicates,oledbdatareader,C#,Combobox,Duplicates,Oledbdatareader,我想消除OleDbDataReader中的重复项 这应该很容易,但我正在转动我的轮子。任何帮助都将不胜感激 private void Database_Load(object sender, EventArgs e) { try { connection.Open(); OleDbCommand command = new OleDbC

我想消除OleDbDataReader中的重复项 这应该很容易,但我正在转动我的轮子。任何帮助都将不胜感激

        private void Database_Load(object sender, EventArgs e)
        {         
            try
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string query = "select * from PPAPdatabase";
                command.CommandText = query;                

                OleDbDataAdapter da = new OleDbDataAdapter(command);
                dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;

                OleDbDataReader reader = command.ExecuteReader();
                while (reader.Read())
            {
                comboBox_Owner.Items.Add(reader["Owner"].ToString());                   
            }

重构SQL查询,如下所示:

select distinct Owner from PPAPdatabase
而不是

select * from PPAPdatabase
代码中唯一需要的列是Owner,然后只获取该列并对其应用DISTINCT子句以获取该列的DISTINCT值

或更换以下线路:

OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
    comboBox_Owner.Items.Add(reader["Owner"].ToString());                   
}
据此:

var owners = dt.AsEnumerable().Select(row => row["Owner"].ToString()).Distinct();
foreach(var owner in owners)
{
    comboBox_Owner.Items.Add(owner);                   
}
在这个解决方案中,我们对数据库使用一个SQL查询,并将结果重新用于Extrect distinct owners。

您可以这样做

while (reader.Read())
{
    if (!comboBox_Owner.Items.Contains(reader["Owner"].ToString()))
        comboBox_Owner.Items.Add(reader["Owner"].ToString());                  
}

为什么要增加一个阅读器?为什么不使用相同的数据表?我会使用数据库为您完成这项工作。只需为它编写一个合适的select distinct查询。您可以使用LINQ在代码中实现,但在这个示例中没有意义。e、 g.从PPAPdatabase中选择distinct Owner,其中Owner不为null首先使用ComboBox_Owner.Items.Clear清除方法顶部附近的ComboBox,然后将SQL修改为从PPAPdatabase中选择distinct Owner,其中Owner不为null。您只需要填充数据源一次,实际上不需要对阅读器进行填充。使用reader或datasource方法。工作完成:纠正我的错误,但我使用了字符串query=select*fromPPAPDatabase;若要在datagridview中显示所有数据,则可以创建单独的命令。每个命令都有自己的SQL查询。@CodeNotFound您可以编写它吗?@adjan当然可以在从数据库填充之前清除组合框。@NOGRP90我更新了我的答案,然后使用另一种更好的方法。最好清除组合框并从数据库的不同值填充它,如其他答案中所建议的那样。这对你来说是正确的解决方案。@Manodesla是的。可能是这样,但我不知道怎么做:我已经添加了这项技术作为对问题本身的评论。