DataGrid problem

J

JL

Hi all,


I want to highlight the date overdue row in datagid so that it will be easy
for user caution, how to change the specific row background color in
datagrid?


Thx a lot
 
G

George Durzi

dgMyDataGrid.Items[somenumber].Row.Color

or something like that ...

Sorry, no access to VS right now to verify...
 
K

Ken Cox [Microsoft MVP]

Hi JL,

You can catch the ItemDataBound event as each row is getting its data and
make some decisions there.

For example, in the code below, I look for the value of "4" in the first
cell. If I find it, I turn that cell and another red.

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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top