从AutoCompleteStringCollection中删除重复项c#

时间:2013-04-29 10:24:48

标签: c# windows winforms

AutoCompleteStringCollection treatmentCollection = new AutoCompleteStringCollection();
if (con.State == ConnectionState.Closed)
{
    con.Open();
}
SqlCeCommand cmdTreatment = new SqlCeCommand("select Treatment from NewPatient", con);
SqlCeDataReader readerTreatment = cmdTreatment.ExecuteReader();
if (readerTreatment.Read() == true)
{
    while (readerTreatment.Read())
    {
        treatmentCollection.Add(readerTreatment["Treatment"].ToString());
    }
}
comboBox2.AutoCompleteMode = AutoCompleteMode.Suggest;
comboBox2.AutoCompleteSource = AutoCompleteSource.CustomSource;
comboBox2.AutoCompleteCustomSource = treatmentCollection;
if (con.State == ConnectionState.Open)
{
    con.Close();
}

我希望combobox2应该只有不同的价值。 和combobox从treatmentCollection类的AutoCompleteStringCollection对象获取值 请帮帮我?

3 个答案:

答案 0 :(得分:3)

替换:

select Treatment from NewPatient

到此:

select distinct Treatment from NewPatient

答案 1 :(得分:0)

如果只想要不同的值,可以在sql命令中使用DISTINCT,如;

select DISTINCT Treatment from NewPatient

查看Eliminating Duplicates with DISTINCT

答案 2 :(得分:0)

我使用此代码并且工作正常。

string query = @"Select distinct [FieldName] from [tableName]";