Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGridView - Set back color for cell range

Status
Not open for further replies.

RichS

Programmer
Apr 24, 2000
380
US
I am using a DataGridView and would like to programmatically set the backcolor for a range of cells in it, similar to what the Outlook calendar looks like when meetings are displayed in Work Week view. Anyone know how?
 
Something like this ought to do it:

Code:
dataGridView1.Rows[0].Cells[0].Style.BackColor = Color.BurlyWood;

Hope this helps,

Alex

[small]----signature below----[/small]
Who are you, and why should I care?
Side A: We're a community of outdated robots who refused to upgrade and came here
*changes sides*
Side B: to live a simpler existence. Free of technology.
 
That only sets the top left cell. I'm looking to do a range of cells. I'm thinking there must be a way to do this with out looping through a range. I also looked at DataGridViewBand but it did not seem to have something for this either.
 
Why the aversion to looping?

Anyway you can do the same for columns and rows (rows seem to take precedence over columns as far as what color is displayed)

Code:
            dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.BurlyWood;
            dataGridView1.Rows[1].DefaultCellStyle.BackColor = Color.Yellow;

You need to remember though that just because a datagridview looks similar to excel, it is most certainly not.

Hope this helps,

Alex

[small]----signature below----[/small]
Who are you, and why should I care?
Side A: We're a community of outdated robots who refused to upgrade and came here
*changes sides*
Side B: to live a simpler existence. Free of technology.
 
Thanks for the response. And sure it's not Excel, it just seem like functionality should be included. I'll just write a little routine for it myself.

Thanks.
 
Sometimes it would be nice, but it would probably give the DataGridView a lot of excess baggage that it doesn't need (and behind the scenes, it would probably be looping anyways).

If you have any type of helper class for form operations, you might consider adding something like this?

Code:
        public static void highlightRange(DataGridView dR, System.Drawing.Point p1, System.Drawing.Point p2, System.Drawing.Color colr)
        {
            //reset to white
            foreach (DataGridViewRow drw in dR.Rows)
            {
                foreach (DataGridViewCell drc in drw.Cells)
                {
                    drc.Style.BackColor = System.Drawing.Color.White;
                }
            }
            //highlight desired range
            for (int i = p1.Y; i <= p2.Y; i++)
            {
                for (int n = p1.X; n <= p2.X; n++)
                {
                    dR.Rows[i].Cells[n].Style.BackColor = colr;
                }
            }
        }

This will save you from having to code the same thing over and over (if this is a common requirement).

Hope this helps,

Alex


[small]----signature below----[/small]
Who are you, and why should I care?
Side A: We're a community of outdated robots who refused to upgrade and came here
*changes sides*
Side B: to live a simpler existence. Free of technology.
 
Cool, thanks Alex. (I also note the reference to Futurama, one of my fav's)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top