---
title: "How to set DataGridView cell style unchangeable"  
description: "How to set DataGridView cell style unchangeable"  
author: "Samuel Fernandes"  
published: 2014-03-06  
updated: 2014-03-06  
canonical: https://www.mindstick.com/forum/1980/how-to-set-datagridview-cell-style-unchangeable  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# How to set DataGridView cell style unchangeable

I'm [trying](https://answers.mindstick.com/qa/93698/6-mistakes-couples-are-trying-to-save-money) to set different back [color](https://www.mindstick.com/articles/77/how-to-split-form-background-color-in-c-sharp) of my DayaGridview rows by [specific column](https://www.mindstick.com/forum/34001/how-to-get-a-specific-column-value-from-a-datatable)

**This is my [Code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii):**

```
private void SetFormat_dgv_Scheduled(string columnsname)    {        dgv_Scheduled.Sort(dgv_Scheduled.Columns[columnsname], ListSortDirection.Ascending);        Color clr = Color.Red;        string dt = (Convert.ToDateTime(dgv_Scheduled.Rows[0].Cells[columnsname].Value)).ToShortDateString();        foreach(DataGridViewRow row in dgv_Scheduled.Rows)        {            if (Convert.ToDateTime(row.Cells[columnsname].Value).ToShortDateString() != dt)            {                if(clr==Color.Red)                {                    clr = Color.LightBlue;                }                else                {                    clr = Color.Red;                }            }                  row.DefaultCellStyle.BackColor = clr;        }              }
```

It [works fine](https://answers.mindstick.com/qa/115732/my-software-works-fine-on-windows-10-but-crashes-on-windows-11-why).

But when I [click](https://www.mindstick.com/articles/12423/social-login-magento-2-one-click-to-register-social) [header](https://www.mindstick.com/articles/336036/explain-about-html-header-body-and-footer-tags) to autosort, all [format](https://www.mindstick.com/forum/162083/how-to-convert-ost-files-into-pst-format) will set to [default](https://www.mindstick.com/interview/12771/what-is-the-importance-of-default-resources) style.

(All red rows change back to white)

Is there any way that I can set format unchangeable?

Thanks.

## Replies

### Reply by Pravesh Singh

Hi Samuel,\

Try calling SetFormat_dgv_Scheduled method in CellFormatting Event as shown

```
   private void SetFormat_dgv_Scheduled(string columnsname)    {        //dataGridView1.Sort(dataGridView1.Columns[columnsname], ListSortDirection.Ascending);        Color clr = Color.Red;        string dt = Convert.ToDateTime(dgv_Scheduled.Rows[0].Cells[columnsname].Value)).ToShortDateString();        foreach (DataGridViewRow row in dgv_Scheduled.Rows)        {            if (Convert.ToDateTime(row.Cells[columnsname].Value).ToShortDateString() != dt)            {                if (clr == Color.Red)                {                    clr = Color.LightBlue;                }                else                {                    clr = Color.Red;                }            }            row.DefaultCellStyle.BackColor = clr;        }    }    private void dgv_Scheduled_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)    {        SetFormat_dgv_Scheduled("columnsname");    }
```


---

Original Source: https://www.mindstick.com/forum/1980/how-to-set-datagridview-cell-style-unchangeable

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
