DataGrid - ItemDataBound

  • Thread starter =?iso-8859-1?Q?Andr=E9_Almeida_Maldonado?=
  • Start date
?

=?iso-8859-1?Q?Andr=E9_Almeida_Maldonado?=

Hey Guys,

I need to manipulate the data that is binding to a datagrid BEFORE its appearance to the user. Because I need to format it inside an If Clause.

I was putting my code in the ItemDataBound Event:

Dim shoCont As Integer

Try
Long.Parse(e.Item.Cells(1).Text)
Catch ex As Exception
Exit Sub
End Try

If e.Item.Cells(1).Text <> e.Item.Cells(2).Text Then
For shoCont = 0 To e.Item.Cells.Count
e.Item.Cells(2).ForeColor = System.Drawing.Color.Red
lngTotalDifi += Long.Parse(e.Item.Cells(2).Text) - Long.Parse(e.Item.Cells(1).Text)
Next
End If


BUT IT ISN'T WORKING????
WHAT IS WRONG???

THANK'S
 
K

Ken Cox [Microsoft MVP]

Hi André,

I used your code in my own page and it worked fine with a minor addition.
Here's what I used in case you need to compare:

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.AlternatingItem _
Or e.Item.ItemType = ListItemType.Item Then
Dim shoCont As Integer
Dim lngTotalDifi As Long
Dim strText As String
Try
strText = e.Item.Cells.Item(1).Text()
Long.Parse(e.Item.Cells(1).Text)
Catch ex As Exception
Exit Sub
End Try

If e.Item.Cells(1).Text <> e.Item.Cells(2).Text Then
For shoCont = 0 To e.Item.Cells.Count
e.Item.Cells(2).ForeColor = System.Drawing.Color.Red
lngTotalDifi += Long.Parse(e.Item.Cells(2).Text) _
- Long.Parse(e.Item.Cells(1).Text)
Next
End If
End If
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("StringValue2", GetType(String)))
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("IntegerValue2", 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) = "Item2 " + i.ToString()
dr(1) = i
dr(2) = i * i
dr(3) = "Item " + i.ToString()
dr(4) = 1.23 * (i + 1)
dr(5) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

Ken
Microsoft MVP [ASP.NET]


Hey Guys,

I need to manipulate the data that is binding to a datagrid BEFORE its
appearance to the user. Because I need to format it inside an If Clause.

I was putting my code in the ItemDataBound Event:

Dim shoCont As Integer

Try
Long.Parse(e.Item.Cells(1).Text)
Catch ex As Exception
Exit Sub
End Try

If e.Item.Cells(1).Text <> e.Item.Cells(2).Text Then
For shoCont = 0 To e.Item.Cells.Count
e.Item.Cells(2).ForeColor = System.Drawing.Color.Red
lngTotalDifi += Long.Parse(e.Item.Cells(2).Text) -
Long.Parse(e.Item.Cells(1).Text)
Next
End If


BUT IT ISN'T WORKING????
WHAT IS WRONG???

THANK'S
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top