Set the background colour of a grid

J

John Doe

Hi
Is it possible to set the background colour of a grid
based on a value in data?
I guess that it is simple but I have been unable to figure
it out.

TIA
 
A

Alvin Bruney

What you want is the BackColor property on the DataGrid object. It takes a
Color enum
Color.Red for example

regards
 
J

John Doe

Hi Alvin

I am just learning this, I have a grid that has a
DataTable as the source.

I have created the following function:
private void itemcreated(object sender,
DataGridItemEventArgs e)
{
if(((DataRowView).e.Item.DataItem).ItemArray[5].ToString
() == "0")
{
e.Item.Cells[5].BackColor = System.Drawing.Color.Red;
}
}

I have then specified that the datagrid should use this
function when OnItemCreated is fired.

This gives me an access violation that I guess is due to
the fact that I have a DataTable as the source. I think
that I am on the right track since removing the if
statement colors the column red as I want.

TIA
 
J

John Doe

Solved it myself. Used the following code:

private void Item_Bound(object sender,
DataGridItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) ||
(e.Item.ItemType == ListItemType.AlternatingItem))
{
if(e.Item.Cells[5].Text !="0")
{
e.Item.Cells[5].BackColor = System.Drawing.Color.Red;
}
}
}

Then I specified the following property on the datagrid
OnItemDataBound="Item_Bound"

-----Original Message-----
Hi Alvin

I am just learning this, I have a grid that has a
DataTable as the source.

I have created the following function:
private void itemcreated(object sender,
DataGridItemEventArgs e)
{
if(((DataRowView).e.Item.DataItem).ItemArray[5].ToString
() == "0")
{
e.Item.Cells[5].BackColor = System.Drawing.Color.Red;
}
}

I have then specified that the datagrid should use this
function when OnItemCreated is fired.

This gives me an access violation that I guess is due to
the fact that I have a DataTable as the source. I think
that I am on the right track since removing the if
statement colors the column red as I want.

TIA
 
A

Alvin Bruney

substitute for the if part:
if(e.Item.Cells[5].Text = "0")
//color cell

--


-----------
Got TidBits?
Get it here: www.networkip.net/tidbits/default.htm

John Doe said:
Hi Alvin

I am just learning this, I have a grid that has a
DataTable as the source.

I have created the following function:
private void itemcreated(object sender,
DataGridItemEventArgs e)
{
if(((DataRowView).e.Item.DataItem).ItemArray[5].ToString
() == "0")
{
e.Item.Cells[5].BackColor = System.Drawing.Color.Red;
}
}

I have then specified that the datagrid should use this
function when OnItemCreated is fired.

This gives me an access violation that I guess is due to
the fact that I have a DataTable as the source. I think
that I am on the right track since removing the if
statement colors the column red as I want.

TIA
-----Original Message-----
What you want is the BackColor property on the DataGrid object. It takes a
Color enum
Color.Red for example

regards

--


-----------
Got TidBits?
Get it here: www.networkip.net/tidbits/default.htm




.
 
J

John Doe

Thanks Alvin
-----Original Message-----
substitute for the if part:
if(e.Item.Cells[5].Text = "0")
//color cell

--


-----------
Got TidBits?
Get it here: www.networkip.net/tidbits/default.htm

Hi Alvin

I am just learning this, I have a grid that has a
DataTable as the source.

I have created the following function:
private void itemcreated(object sender,
DataGridItemEventArgs e)
{
if(((DataRowView).e.Item.DataItem).ItemArray [5].ToString
() == "0")
{
e.Item.Cells[5].BackColor = System.Drawing.Color.Red;
}
}

I have then specified that the datagrid should use this
function when OnItemCreated is fired.

This gives me an access violation that I guess is due to
the fact that I have a DataTable as the source. I think
that I am on the right track since removing the if
statement colors the column red as I want.

TIA
-----Original Message-----
What you want is the BackColor property on the DataGrid object. It takes a
Color enum
Color.Red for example

regards

--


-----------
Got TidBits?
Get it here: www.networkip.net/tidbits/default.htm

"John Doe" <[email protected]> wrote
in
message
Hi
Is it possible to set the background colour of a grid
based on a value in data?
I guess that it is simple but I have been unable to figure
it out.

TIA


.


.
 
J

John Doe

Sorry for my silly question. But how do I loop through
each cell in the cells collection and set the background
colour.
 
A

Alvin Bruney

for(int i = 0; i < e.item.cells.count;i++)
e.items.cells.BackColor = Color.red;


roughly
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top