Set the background colour of a grid

  • Thread starter Thread starter John Doe
  • Start date Start date
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
 
What you want is the BackColor property on the DataGrid object. It takes a
Color enum
Color.Red for example

regards
 
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
 
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
 
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




.
 
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


.


.
 
Sorry for my silly question. But how do I loop through
each cell in the cells collection and set the background
colour.
 
for(int i = 0; i < e.item.cells.count;i++)
e.items.cells.BackColor = Color.red;


roughly
 
Back
Top