Users Pricing

forum

Home Forums How to set DataGridView cell style unchangeable – MindStick

How to set DataGridView cell style unchangeable

Samuel Fernandes 2931 06 Mar 2014

I'm trying to set different back color of my DayaGridview rows by specific column

This is my Code:

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.

But when I click header to autosort, all format will set to default style.

(All red rows change back to white)

Is there any way that I can set format unchangeable?

Thanks.


1 Answers

Markdown for AI

A clean, structured version of this page for AI assistants and LLMs.

Open .md