How to link a chekedListBox with a reportviewer recognizing all the selected items?

-1

this is the button that generates the report:

 private void bunifuThinButton21_Click(object sender, EventArgs e)                      
    {
        Datos datos = new Datos();
        AsesoriaFiscal af = new AsesoriaFiscal();

        datos.Nombre = txtNombre.Text;
        datos.Nif = txtNifCif.Text;
        datos.Direccion = txtDireccion.Text;
        datos.Poblacion = txtPoblacion.Text;
        datos.Provincia = txtProvincia.Text;
        datos.CodigoPostal = txtCodigoPostal.Text;
        datos.Telefono = txtTelefono.Text;
        datos.Email = txtEmail.Text;
        datos.Actividades = Actividad.Text;

        af.datos.Add(datos);
        af.Show();           
    }

And this is the class Data:

   namespace Operaciones
 {
       public class Datos
   {
    public string Nombre { get; set; }
    public string Nif { get; set; }
    public string Direccion { get; set; }
    public string Poblacion { get; set; }
    public string Provincia { get; set; }
    public string CodigoPostal { get; set; }
    public string Telefono { get; set; }
    public string Email { get; set; }
    public string Actividades { get; set; }
    }
   }

All this is correct. But when I try it with a chekedlistbox, it only recognizes one item (the chekedlistbox are all those that start with chlb:

    private void bunifuThinButton21_Click(object sender, EventArgs e)
    {

        SeguridadDatos datos1 = new SeguridadDatos();
        InformeSeguridad ag = new InformeSeguridad();


        datos1.SeguridadFisica = chlbSeguridadFisicaInstalaciones.SelectedItem.ToString();
        datos1.DescripcionDeLosSistemas = chlbDescripcionSistemas.CheckedItems.ToString();
        datos1.ServidorDedicado = chlbServidorDedicado.CheckedItems.ToString();
        datos1.ServidorNoDedicado = chlbServidorNoDedicado.CheckedItems.ToString();
        datos1.OrdenadoresPersonales = chlbOrdenadoresPersonales.CheckedItems.ToString();
        datos1.ServidorCorreoElectronico = chlbServidorCorreoElectronico.CheckedItems.ToString();
        datos1.ServidorPaginaWeb = chlbServidorPaginaWeb.CheckedItems.ToString();
        datos1.EquiposInformaticos = chlbEquiposInformaticos.CheckedItems.ToString();


        ag.informe.Add(datos1);
        ag.Show();
    }
    
asked by Mante 01.06.2018 в 13:46
source

2 answers

1

If you have to show all the items with the check activated separated with a carriage return, you can use the following:

datos1.SeguridadFisica = String.Join("\n", chlbSeguridadFisicaInstalaciones.CheckedItems.Cast<String>());

This links the items with the check enabled using \n which is a carriage return.

If you already need to appear in a table, you must change the report to enter that table and you would have to pass the items in some structure, for example a DataSet.

    
answered by 04.06.2018 в 08:58
0

What I understand is that you need to obtain the list of those Data that are checked:

//Lista auxiliar donde introduciras solo los items seleccionados
List<Datos> listaChecked = new List<Datos>(); 

//loop de 0 hasta la longitud de tu checkedlistbox
for(int i=0 ; i<checkedListBox1.Items.Count ; i++){
    //Va item a item comprobando que el estado sea checked
    if(checkedListBox1.GetItemCheckState(i) == CheckState.Checked)
    {
        //Si está checkeado añade a la lista
        listaChecked.add(checkedListBox.Items[i]);
    }
}

And then you add listChecked to the reportViewer. If you have doubts with this last one I need the part of the code referring to adding the data to the reportViewer.

    
answered by 01.06.2018 в 14:20