Change color of a given cell

Asked

Viewed 4,652 times

1

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

I need to paint just one cell, getting like this:

inserir a descrição da imagem aqui

how could I do that?

I already have the following methods:

    DateTime diaAgenda = DateTime.Now;
    DateTime diaInicioAgenda;
    DateTime diaFimAgenda;

    public void carregaTela()
    {         
        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;       
    }
  • 1

    Is there a specific way? Are you having some difficulty? Is there a code where you should apply?

  • @bigown, I changed the question!

1 answer

3


Thus

dataGridView.Rows[rowIndex].Cells["nomeDaColuna"].Style.BackColor = Color.Yellow;
  • I’d put that where?

  • You have to know, right? When do you want to change the color of the cell? By clicking a button? By loading the screen? In any specific action?

  • when loading the screen.

  • So put in the event carregaTela(), but do not forget that the grid needs to be populated

  • It worked, yeah. I just don’t know if it’ll help me in whatever way I need it, but because I didn’t do it the right way...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.