How can I set the first line of Combobox empty in my code? This is the function that is called when the form loads and it inserts the options on the Combobox.
private void preencherComboDpt()
{ carregandoComboDepartamentos = true;
NpgsqlConnection conn = new NpgsqlConnection();
try
{
conn = new NpgsqlConnection(connstring);
string query = "select * from departamento order by departamento";
NpgsqlCommand cmd = new NpgsqlCommand(query, conn);
cmd.CommandText = query;
conn.Open();
NpgsqlDataReader drd = cmd.ExecuteReader();
DataTable dt = new DataTable("tabela");
dt.Columns.Add("departamento_id", typeof(int));
dt.Columns.Add("departamento", typeof(string));
while (drd.Read())
{
DataRow row = dt.NewRow();
row["departamento_id"] = int.Parse(drd["departamento_id"].ToString());
row["departamento"] = drd["departamento"].ToString();
dt.Rows.Add(row);
}
cobUnivDep.DataSource = dt.DefaultView;
cobUnivDep.DisplayMember = "departamento";
cobUnivDep.ValueMember = "departamento_id";
}
catch
{
MessageBox.Show("Error ");
}
carregandoComboDepartamentos = false;
}
I already tried inserting the line cobUnivDep.SelectedIndex = null, but it doesn't work.