469,292 Members | 1,310 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,292 developers. It's quick & easy.

Setting the BackColor of indvidual cells in a DataGridView

Hello All,

I have been trying to figure out how to set the BackColor of
individual cells in a DataGridView. I found the following solution in
a VB usenet group
>>>>>
DataGridView1.Item(ColumnIndex, RowIndex).Style.BackColor = Color
DataGridView1.Item(ColumnIndex, RowIndex).Style.ForeColor = Color

or

DataGridView1.CurrentCell.Style.BackColor = Color
DataGridView1.CurrentCell.Style.ForeColor = Color
>>>>>
I figured I could just change the () to [] and it would work in C#.
Howver, the DataGridView does not seem to have an Item property. How
can I accomplish this using C#?

Thanks,

Paul

Feb 9 '07 #1
4 31619
The syntax is to use the indexer directly on the DataGridView and not
on an Items property.

Me.DataGridView1(2, 3).Style.BackColor = Color.Red

================
Clay Burch
Syncfusion, Inc.

Feb 10 '07 #2
The C# syntax is

this.dataGridView1[2,3].Style.BackColor = Color.Red;

===============
Clay Burch
Syncfusion, Inc.

Feb 10 '07 #3
You have to do it through the DefaultCellStyle property.

DataGridViewCellStyle MakeItRed = new DataGridViewCellStyle;
MakeItRed.BackColor = Color.Red;

//make a whole column red
myGrid.Columns(1).DefaultCellStyle = MakeItRed;

//make a specific cell red
DataGridViewRow row2 = myGrid.Rows(2);
row2.Cells(2).Style = MakeItRed

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
-----------------------------------------------
"Paul" <he********@yahoo.cawrote in message
news:11**********************@a34g2000cwb.googlegr oups.com...
Hello All,

I have been trying to figure out how to set the BackColor of
individual cells in a DataGridView. I found the following solution in
a VB usenet group
>>>>>>
DataGridView1.Item(ColumnIndex, RowIndex).Style.BackColor = Color
DataGridView1.Item(ColumnIndex, RowIndex).Style.ForeColor = Color

or

DataGridView1.CurrentCell.Style.BackColor = Color
DataGridView1.CurrentCell.Style.ForeColor = Color
>>>>>>

I figured I could just change the () to [] and it would work in C#.
Howver, the DataGridView does not seem to have an Item property. How
can I accomplish this using C#?

Thanks,

Paul

Feb 10 '07 #4
Actually, I should have said, "You *can* do it through the DefaultCellStyle
property".

Robin S.
--------------------------
"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:RN******************************@comcast.com. ..
You have to do it through the DefaultCellStyle property.

DataGridViewCellStyle MakeItRed = new DataGridViewCellStyle;
MakeItRed.BackColor = Color.Red;

//make a whole column red
myGrid.Columns(1).DefaultCellStyle = MakeItRed;

//make a specific cell red
DataGridViewRow row2 = myGrid.Rows(2);
row2.Cells(2).Style = MakeItRed

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
-----------------------------------------------
"Paul" <he********@yahoo.cawrote in message
news:11**********************@a34g2000cwb.googlegr oups.com...
>Hello All,

I have been trying to figure out how to set the BackColor of
individual cells in a DataGridView. I found the following solution in
a VB usenet group
>>>>>>>
DataGridView1.Item(ColumnIndex, RowIndex).Style.BackColor = Color
DataGridView1.Item(ColumnIndex, RowIndex).Style.ForeColor = Color

or

DataGridView1.CurrentCell.Style.BackColor = Color
DataGridView1.CurrentCell.Style.ForeColor = Color
>>>>>>>

I figured I could just change the () to [] and it would work in C#.
Howver, the DataGridView does not seem to have an Item property. How
can I accomplish this using C#?

Thanks,

Paul


Feb 10 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by michael sorens | last post: by
reply views Thread by Mike P | last post: by
reply views Thread by Steve | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.