Setting the BackColor of indvidual cells in a DataGridView

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

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
 
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.
 
The C# syntax is

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

===============
Clay Burch
Syncfusion, Inc.
 
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 said:
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
 
Actually, I should have said, "You *can* do it through the DefaultCellStyle
property".

Robin S.
--------------------------
RobinS said:
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 said:
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
 
Back
Top