Change color of a given cell

1

I have a DataGridView that has 8 columns (days of the week) and x rows (times of day).

I need to paint only one cell, thus:

HowcouldIdothis?

Ialreadyhavethefollowingmethods:

DateTimediaAgenda=DateTime.Now;DateTimediaInicioAgenda;DateTimediaFimAgenda;publicvoidcarregaTela(){gridAgenda.DataSource=gerarAgenda(diaAgenda);carregarAgenda("10:00", "20:00", diaInicioAgenda, diaFimAgenda);            
    }

    public void tamanhoGrid()
    {
        foreach (DataGridViewColumn column in gridAgenda.Columns)
        {
            if (column.DataPropertyName == "Hora")
                column.Width = 60;
            else
            column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }
    }

    public DataTable gerarAgenda(DateTime dia)
    {
        DataTable tabela = new DataTable();
        Functions.configuracoesGrade(gridAgenda);
        gridAgenda.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

        tabela.Columns.Add("Atendimento");
        tabela.Columns.Add("Hora");

        string diahoje = String.Format("{0:ddd, MMM d}", dia);

        tabela.Columns.Add("" + diahoje);

        for (int data = 1; data < 7; data++)
        {
            dia = dia.AddDays(1);
            diahoje = String.Format("{0:ddd, MMM d}", dia);
            tabela.Columns.Add("" + diahoje);
        }
        return tabela;            
    }

    //metodo para verificar se existe um atendimento para a hora e data
    public void carregarAgenda(String HoraInicial, String HoraFinal, DateTime dataInicial, DateTime dataFinal)
    {
        AgendaBLL agenda = new AgendaBLL();
        AgendaModel objAgenda = new AgendaModel();

        verificarDia();
        tamanhoGrid();

        diaInicioAgenda = DateTime.ParseExact(gridAgenda.Columns[2].HeaderText.ToString(), "ddd, MMM d", new CultureInfo("pt-BR"));
        diaFimAgenda = DateTime.ParseExact(gridAgenda.Columns[8].HeaderText.ToString(), "ddd, MMM d", new CultureInfo("pt-BR"));

        listaAgenda = agenda.carrega(diaInicioAgenda, diaFimAgenda, Profi);

        TimeSpan horaI = new TimeSpan(10, 00, 00);
        TimeSpan horaF = new TimeSpan(22, 00, 00);         

        //Para cada hora do dia
        for (TimeSpan data = horaI; data <= horaF; )
        {
            foreach (var list in listaAgenda)
            {
                var hora = list.Atendimento.Hora.ToString();
                int compareTime = TimeSpan.Compare(list.Atendimento.Hora, data);           

                // Hora do Atendimneto MENOR que a data da agenda
                if (compareTime == -1)
                {

                }
                //Hora do atendimento IGUAL a hora da agenda
                if (compareTime == 0)
                {
                    adicionarRow(list, data);
                }

                //Hora do atendimento MAIOR a hora da agenda
                if (compareTime == 1)
                {
                    //adicionarRow(list);
                }
            }

            objAgenda.Atendimento.Hora = data;
            listaAgenda.Add(objAgenda);
            adicionarRow(objAgenda, data);

            var trintaMin = new TimeSpan(0, 30, 0);
            data = data.Add(trintaMin);
        }
    }

    private void adicionarRow(AgendaModel a, TimeSpan data)
    {
        DataTable dt = (DataTable)gridAgenda.DataSource;            
        DataRow l = dt.NewRow();           

        gridAgenda.Columns["Atendimento"].Visible = false;
        l["Atendimento"] = a.Atendimento.ID_Atendimento_Item;

        var horaFormatada = string.Format("{0:hh\:mm}", a.Atendimento.Hora);          

        l["Hora"] = horaFormatada;


        foreach (DataGridViewColumn coluna in gridAgenda.Columns)
        {
            if (coluna.HeaderText != "Hora")
            {
                String dataAgenda = String.Format("{0:M/d/yyyy}", coluna.HeaderText);
                String dataServico = String.Format("{0:ddd, MMM d}", a.Atendimento.Data_Servico);

                if (dataServico == dataAgenda)
                {
                    l[dataAgenda] = a.AgendaResumida;
                    //dt.Rows[0]["atendimento"] = a.AgendaResumida;
                    //dt.Rows[data][dataAgenda].Value = a.AgendaResumida;

                }
            }
        }
        dt.Rows.Add(l);
        gridAgenda.DataSource = dt;       
    }
    
asked by anonymous 25.11.2015 / 20:36

1 answer

3

So

dataGridView.Rows[rowIndex].Cells["nomeDaColuna"].Style.BackColor = Color.Yellow;
    
25.11.2015 / 20:41