problem fill datagridview c #

1

hello I have a problem filling the datagrid with the data from my BD, the datagrid is a schedule grid with the days of the week, the problem is that I only fill one cell of the datagrid for each day, that is, if I have in the BD three schedules loaded for the same day only shows me 1 (neither the first nor the last, shows any of that day).

  

Clarifications about things tested at the moment: The query to the BD returns all the values, in the conditional it enters all the times which is correct but I do not know why it does not load the values in the cells, that is to say if I have 10 loaded values in my BD and enter the 10 times to the conditional but only shows some values.

I attach parts of the code

  if (query.Count() > 0)
        {
            int contador = 0;
            foreach (var item in query)
            {

                DateTime DiaTurno = new DateTime(myCal.GetYear(item.fecha_turno), myCal.GetMonth(item.fecha_turno), myCal.GetDayOfMonth(item.fecha_turno), new GregorianCalendar());

                int dia = (int)DiaTurno.DayOfWeek;

                string horario_turno = item.fecha_turno.ToString("HH:mm");


                for (int fila = 0; fila <= AgendaSemanal.Rows.Count - 1; fila++)
                {

                   /*EN ESTE CONDICIONAL ENTRA TODAS LAS VECES QUE DEBE PERO NO ME COMPLETA TODAS LAS CELDAS, SOLO ME COMPLETA 1 POR DIA*/
                        if (AgendaSemanal.Rows[fila].Cells[0].Value.ToString() == horario_turno)
                        {
                            AgendaSemanal.Rows[fila].Cells[dia+1].Value = item.nombre + " " + item.apellido;
                            AgendaSemanal.Rows[fila].Cells[dia + 1].Style.BackColor=System.Drawing.Color.Salmon;
                            AgendaSemanal.Rows[fila].Cells[dia + 1].Style.ForeColor = System.Drawing.Color.White;

         }
                        else
                             {

                             AgendaSemanal.Rows[fila].Cells[dia + 1].Value = "Libre";
                             AgendaSemanal.Rows[fila].Cells[dia + 1].Style.BackColor = System.Drawing.Color.Bisque;
                             AgendaSemanal.Rows[fila].Cells[dia + 1].Style.ForeColor = System.Drawing.Color.Black;
                            }


                }

            }



        }
    
asked by fer 28.12.2017 в 00:53
source

2 answers

0

I think your problem is in the else part. For each item of the query you fill in the grid and do not check if the row is occupied.

You can try this else

 else
     {
     //compruebas que no tenga valor asignado la celda
     //aquí tu siempre escribes libre tenga valor o no
     if (this.AgendaSemanal.Rows[0].Cells[0].Value!=DBNull) //o con null o con lo que lo inicialices
       {
       AgendaSemanal.Rows[fila].Cells[dia + 1].Value = "Libre";
       AgendaSemanal.Rows[fila].Cells[dia + 1].Style.BackColor = System.Drawing.Color.Bisque;
       AgendaSemanal.Rows[fila].Cells[dia + 1].Style.ForeColor = System.Drawing.Color.Black;
       }
     }

Greetings

    
answered by 28.12.2017 / 15:32
source
0

My solution was to remove the else that gave problems and choose to fill all the datagrid with "Free" values and then load the data from the database, in any case I highlight the solution of @Juanjo

//PRIMERO LLENO EL DATAGRID

 for (int fila = 0; fila <= AgendaSemanal.Rows.Count - 1; fila++)
        {   
            for (int col=1; col<AgendaSemanal.Columns.Count;col++)
            {
                AgendaSemanal.Rows[fila].Cells[col].Value = "Libre";
                AgendaSemanal.EnableHeadersVisualStyles = false;
                AgendaSemanal.ColumnHeadersDefaultCellStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#cddfe1");
                AgendaSemanal.Rows[fila].Cells[col].Style.BackColor = System.Drawing.Color.WhiteSmoke;
                AgendaSemanal.Rows[fila].Cells[0].Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#cddfe1");
            }             


        }

//VERIFICO SI OBTUVE TURNOS REGISTRADOS EN LA BD
     if (query.Count() > 0)
        {
           //EN CASO DE QUE HAYA TURNOS RECORRO CADA UNO DE LOS TURNOS
            foreach (var item in query)
            {
                //ARMO EL DIA DEL TURNO SACADO DE LA BD
                DateTime DiaTurno = new DateTime(myCal.GetYear(item.fecha_turno), myCal.GetMonth(item.fecha_turno), myCal.GetDayOfMonth(item.fecha_turno), new GregorianCalendar());

                int dia = (int)DiaTurno.DayOfWeek;

                 // EL HORARIO SACADO DE LA BD LO CONVIERTO A STRING PARA QUE TENGA EL MISMO FORMATO QUE LAS FILAS DE HORARIOS DEL DATAGRID 
                 // ENTONCES AHI PUEDO COMPARAR ENTRE ELLOS
                string horario_turno = item.fecha_turno.ToString("HH:mm");
                //RECORRO TODAS LAS FILAS DEL DATA GRID, LAS COLUMNAS NO NECESITO RECORRERLAS YA QUE SU POSICION LAS DETERMINO CON EL NUMERO DE DIA DE LA SEMANA
                for (int fila = 0; fila <= AgendaSemanal.Rows.Count - 1; fila++)
                {

                  //HAGO COMPARACION DE HORARIOS ENTRE LA HORA DEL TURNO Y LA HORA DE CADA FILA DEL DATAGRID
                        if (AgendaSemanal.Rows[fila].Cells[0].Value.ToString() == horario_turno)
                        { //LLENO LA CELDA CON EL NOMBRE COMPLETO DEL PACIENTE
                            AgendaSemanal.Rows[fila].Cells[dia+1].Value = item.nombre + " " + item.apellido;

                         int resultado = DateTime.Compare(item.fecha_turno, DateTime.Now);//HAGO UNA COMPARACION ENTRE LA FECHA/HORA DEL TURNO Y LA FECHA/HORA ACTUAL
                        if (resultado <  0)// si el turno ya paso lo pongo de otro color
                        {
                            AgendaSemanal.Rows[fila].Cells[dia + 1].Style.BackColor = System.Drawing.Color.Salmon;

                        }
                        else
                        {
                            if (PrimeraAtencion(item.idpaciente) == 1)// si retorna 1 entonces ya fue atendido antes
                            {
                                AgendaSemanal.Rows[fila].Cells[dia + 1].Style.BackColor = System.Drawing.Color.LightGreen;
                                AgendaSemanal.Rows[fila].Cells[dia + 1].Style.ForeColor = System.Drawing.Color.Black;
                            }
                            //caso contrario es la primera vez que se atiende
                            else
                            {
                                AgendaSemanal.Rows[fila].Cells[dia + 1].Style.BackColor = System.Drawing.Color.Yellow;
                                AgendaSemanal.Rows[fila].Cells[dia + 1].Style.ForeColor = System.Drawing.Color.Black;
                            }
                        }


                        }


                }

            }
    
answered by 30.12.2017 в 18:30