Fill in Combobox without repetitions

Asked

Viewed 506 times

1

I’m trying to fill one ComboBox from a db, but the ComboBox is filled with many repeated items!

OleDbConnection Con = new OleDbConnection();
Con.ConnectionString = Properties.Settings.Default.dbCombo;

Con.Open();
OleDbCommand Cmm = new OleDbCommand();
Cmm.CommandText = "SELECT NomeInv FROM tbFev;
Cmm.CommandType = CommandType.Text;
Cmm.Connection = Con;
OleDbDataReader DR;
DR = Cmm.ExecuteReader();

DataTable dt = new DataTable();
dt.Load(DR);

DataView dv = new DataView(dt, "", "NomeInv", DataViewRowState.OriginalRows);
comboBox1.DataSource = dv;
comboBox1.DisplayMember = "NomeInv";
comboBox1.ValueMember = "";

Con.Close();

This way is coming all the information from the field NomeInv, even repeated.

Is there a code to prevent this ?

  • I put as an answer Maurice, to stay in the right place and as answered your question.

1 answer

2


Use a DISTINCT in his SELECT.

In this way:

SELECT DISTINCT NomeInv FROM tbFev;

All completely identical lines will be joined by the expression "distinct".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.