472,960 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,960 software developers and data experts.

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 31941
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: michael sorens | last post by:
Is it possible to store an unbound DataGridView component into a setting? I naively tried defining a Setting that is a DataGridView called DGV, then simply assigning it:...
0
by: Mike P | last post by:
I have a number of colours in a database linked to different conditions, and this data is displayed in a gridview. I want to set the BackColor of a gridview cell dependent upon this value, for...
1
by: sklett | last post by:
I've got a strange situation here. I have a databound DataGridView that also has un-bound columns. When the view loads, I want to update the values of the unbound columns. If I attempt to modify...
0
by: Rick | last post by:
I can't find an application setting property for the DataGridView.AlternatingRowsDefaultCellStyle.BackColor. Am I missing something, or did MS just not add this option? I know how to do this...
0
by: Steve | last post by:
Hi All I am using VB.net 2005 professional I set the background color of a datagridview cell to different colors depending on many reasons I set them in the cellformatting event and all...
1
by: Steve | last post by:
Hi All I am using VB.net 2005 professional I set the background color of a datagridview cell to different colors depending on many reasons I set them in the cellformatting event and all...
5
by: davidson1 | last post by:
Hai Friends, I have the following code for Dynamically Created Tables, I have created the panel in the webform....... Dim tab5 As New Table Dim row1 As New TableRow Dim cell1 As...
1
by: ravitunk | last post by:
hello..am developing a windows application which has a datagridview control with many columns.......i have a problem with changing the backcolor of a particular cell based on a specific...
7
by: bobk | last post by:
My apologies upfront for what I am sure is a newbie question, but I have been unable to find an answer via search. I am a VB6 programmer trying to write programs in VB 2008 Express. I have an...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.