Leave a default value in a ComboBox

0

Good day this time my question is a bit simple but forget how to do it, it turns out that I am loading a ComboBox with the information contained in a tabla , the Combo is loaded correctly, I just want to add to this as the default option the legend " Seleccione " before all other items that are loaded from the base de datos , if someone can remind me how to do it, I would greatly appreciate it.

I enclose the method that I did in order to load the Combo.

    public void CargarDepartamentos()
    {
        try
        {
            dtInfoDeptos = objValidaEmpleados.Departamentos();
            if (dtInfoDeptos.Rows.Count>0)
            {
                cmbDepartamento.DataSource = dtInfoDeptos;
                cmbDepartamento.ValueMember = "Dpto_ID";
                cmbDepartamento.DisplayMember = "Dpto_Descripcion";
            }
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }
    
asked by Alberto Arenas 26.05.2017 в 18:24
source

4 answers

3

With this code you can do it

cmbDepartamento.Items.Insert(0, "Seleccionar");
cmbDepartamento.SelectedIndex = 0;

Greetings,

    
answered by 26.05.2017 в 18:35
1

Well I solved it this way

public void CargarDepartamentos()
    {
        try
        {
            dtInfoDeptos = objValidaEmpleados.Departamentos();
            if (dtInfoDeptos.Rows.Count>0)
            {
                datatable tbl = dtInfoDeptos;
                datarow fila=tbl.newrow();
                fila["Dpto_Descripcion"]="Seleccionar";
                tbl.rows.insertat(fila,0);

                cmbDepartamento.ValueMember = "Dpto_ID";
                cmbDepartamento.DisplayMember = "Dpto_Descripcion";
                cmbDepartamento.DataSource = tbl;
            }
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }
    
answered by 19.09.2018 в 18:41
0
  • Select the combobox in the design view.
  • Right click properties.
  • Then look for the one that says text: write the text you want "Select"
  • answered by 26.05.2017 в 18:51
    -3

    In the form's Load:

    cmbDepartamento.Text=("Seleccionar");
    
        
    answered by 27.05.2017 в 04:28