DataGrid Question

G

Guest

I'm trying to use a datagrid to display data and that has been easy - what
i'm trying to do is have a rows background color change to red if the first
databound column is 0 for the item. I have entered an onitemcreated sub
routine to handle the data item when its added to the grid but it hasn't
worked. This is my sub below:

Sub OnCreatedItem(ByVal sender As Object, ByVal e As
DataGridItemEventArgs)

If e.Item.Cells(0).Text = "0" Then
UnitLists.Items(e.Item.ID).BackColor =
System.Drawing.Color.ForestGreen
'e.Item.BackColor = System.Drawing.Color.ForestGreen
End If
End Sub

I was told by another user to use the ItemDataBinding event and to use the
following code:

Dim rw as DataRowView = e.item.DataItem
if rw!=null
rw(0).Equals("0")
or something similar this

But the e variable that has to be passed is for type EventArgs and does not
contain data for the table rows that are created. Any more ideas?
How do i code this so I can vary the background colors for the cells
depending on the data that it contains?

Thank you for your help,
Ryan
 
G

garethdjames

this is how to do it, sorry its in C#

You need to check the value of the dataitem rather than the text as it
would not have been databinded yet

private void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs
e)
{
if(e.Item.ItemType == ListItemType.AlternatingItem
|| e.Item.ItemType == ListItemType.Item)
{
//cast the DataItem to an int (if that is what it is)
if(((int)e.Item.DataItem) == 0)
{
foreach(TableCell cell in e.Item.Cells)
cell.BackColor = Color.Red;
}
}
}
 
E

Eliyahu Goldin

ItemDataBound event. e.Item represents a row. It can be a header, a footer,
a datarow, some other types exist.

e.Item.Cells is a collection of cells inside the row. Find your data there.

Eliyahu
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top