Home > Enterprise >  Unable to change the Fore color of DataGridview cells
Unable to change the Fore color of DataGridview cells

Time:02-27

I have a Winforms application that was designed in the next manner: Shell Form (Main form) with panel control shows child forms on it by clicking on buttons .

    private void btnInbox_Click(object sender, EventArgs e)
    {
        OpenChildForm(new InboxForm(), sender);            
    }

    private Button currentButton;
    private Form activeForm;

    private void OpenChildForm(Form childForm, object btnSender)
    {
        if (activeForm != null)
        {
            activeForm.Close();
        }
        ActivateButton(btnSender);
        activeForm = childForm;
        childForm.TopLevel = false;
        childForm.FormBorderStyle = FormBorderStyle.None;
        childForm.Dock = DockStyle.Fill;
        this.panelDesktop.Controls.Add(childForm);
        this.panelDesktop.Tag = childForm;
        childForm.BringToFront();
        childForm.Show();

    }

In the InboxForm I have the DataGridView (dgvRequests) and in Load event of InboxForm, I'm realizing RefreshDGV() method

private void RefreshDGV()
    {
        //Loading requests from Requests table   to DGV
        SQL_Queries_Inbox q = new SQL_Queries_Inbox();
        dgvRequests.DataSource = q.Get_RequestsFromSql();

        dgvRequests.Columns["ID_Request"].Visible = false;
        dgvRequests.Columns["Purchasing Date"].DefaultCellStyle.Format = "MM/dd/yyyy";
        
        for (int i = 0; i < dgvRequests.Rows.Count; i  )
        {
            DateTime dt = Convert.ToDateTime(dgvRequests.Rows[i].Cells["Purchasing Date"].Value);

            if (dt.Year < 2002)

            {
                dgvRequests.Rows[i].Cells["Purchasing Date"].Style = new DataGridViewCellStyle { ForeColor = Color.Transparent, SelectionForeColor = Color.Transparent };

            }
        }


    }

My trouble is that the Fore color of the needed cell doesn't change, but if I create a new form with the same DataGridview and realize the same Refresh DGV() method everything works fine

enter image description here

  • Related