How to show in a Datagridview a list of objects to choose from in a ComboBox (with different lists as options)

0

I have different lists of different objects and a ComboBox with the names of each list.

How can I do so that each time I choose the name of a list the Datagridview shows the chosen option?

With DataSource I can only select one, as soon as I try to put conditionals for the different selections of Combobox it does not show me anything anymore.

I've tried with this to test if I can choose between these two lists but logically it does not work ....

            cmbEntidades.Items.Add("pelicula");
            cmbEntidades.Items.Add("libro");


            if (cmbEntidades.Text == "libro")
            {
                i = librosPorDefecto();
                dgv1.DataSource = listaLibros;

            }
            else if(cmbEntidades.Text == "pelicula")
            {
                i = peliculasPorDefecto();
                dgv1.DataSource = listaPeliculas;
            }

Thanks !!!

    
asked by dobarqueiro 19.07.2018 в 17:12
source

1 answer

1

Brother in the combo there is an event called selectedIndexChange

you find it when you right click on your combo and property option

now in your combo you have to put the property AutoPostBack AutoPostBack="true"

example:

 <asp:DropDownList ID="CboEstadoCivil" runat="server" class="form-control" AutoPostBack="true" tittle="Seleccione estado civil" OnSelectedIndexChanged="CboEstadoCivil_SelectedIndexChanged">
                                            </asp:DropDownList>




 protected void CboEstadoCivil_SelectedIndexChanged(object sender, EventArgs e)
            {
                if(cmbEntidades.SelectedItem.Text=="libro")
{
 i = librosPorDefecto();
                dgv1.DataSource = listaLibros;
}
if(cmbEntidades.SelectedItem.Text=="pelicula")
{
  i = peliculasPorDefecto();
                dgv1.DataSource = listaPeliculas;
}
            }
    
answered by 19.07.2018 / 19:28
source