Reputation: 35
My excel file is that column1A: city,ankara,ankara,ankara,istanbul,istanbul,izmir I want to that combobox looks like ankara,istanbul,izmir it doesn't again.
OleDbConnection baglan = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0");
baglan.Open();
string sql = "Select * From [Sayfa1$A1:A100] ";
OleDbCommand komut = new OleDbCommand(sql, baglan);
OleDbDataReader dr = null;
dr = komut.ExecuteReader();
while (dr.Read())
{
if (dr[0] != "")
{
combobox1.Items.Add(dr[0].ToString());
}
else
{
break;
}
}
baglan.Close();
Upvotes: 0
Views: 44
Reputation: 187
Try this:
OleDbConnection baglan = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0");
baglan.Open();
string sql = "Select * From [Sayfa1$A1:A100] ";
OleDbCommand komut = new OleDbCommand(sql, baglan);
OleDbDataReader dr = null;
dr = komut.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
// To Copy distinct values from specified column to a different datatable
DataTable diffValues = dt.DefaultView.ToTable(true, "ColName");
combobox1.DataSource = datatable;
It works for me.
Upvotes: 0
Reputation: 2075
if (dr[0] != ""){
if(!combobox1.Items.Contains(dr[0])){
combobox1.Items.Add(dr[0].ToString());
}
}
Upvotes: 2