changing row color depending on a cell value

V

Volkan Karaboða

Hi all

I want to change datagrid's row color depending on a cell value

How can I do this?
I think that I must write some code in bind method but I dont know How I do
this.

please help me!!
thanks...
 
K

Ken Cox [Microsoft MVP]

Hi Volkan,

You need to catch the ItemDataBound event when the row is bound to the
datarow. At that point, you can test if you are dealing with a regular row.
If so, get an instance of the cell you're after and change its colour value.
The sample people should get you started.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub
Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, ByVal e As _
System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
If e.Item.Cells(0).Text = "4" Then
e.Item.Cells(0).BackColor = Color.Red
e.Item.Cells(3).BackColor = Color.Red
End If
End If
End Sub
Function CreateDataSource() As ICollection
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = True
dt.Rows.Add(dr)
Next i
Dim dv As New DataView(dt)
Return dv
End Function 'CreateDataSource
 
V

Volkan Karaboga

thank you very much. It works well...



Ken Cox said:
Hi Volkan,

You need to catch the ItemDataBound event when the row is bound to the
datarow. At that point, you can test if you are dealing with a regular row.
If so, get an instance of the cell you're after and change its colour value.
The sample people should get you started.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub
Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, ByVal e As _
System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
If e.Item.Cells(0).Text = "4" Then
e.Item.Cells(0).BackColor = Color.Red
e.Item.Cells(3).BackColor = Color.Red
End If
End If
End Sub
Function CreateDataSource() As ICollection
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = True
dt.Rows.Add(dr)
Next i
Dim dv As New DataView(dt)
Return dv
End Function 'CreateDataSource

Volkan Karaboða said:
Hi all

I want to change datagrid's row color depending on a cell value

How can I do this?
I think that I must write some code in bind method but I dont know How I
do
this.

please help me!!
thanks...
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top