DataGrid - ItemDataBound

Discussion in 'ASP .Net' started by =?iso-8859-1?Q?Andr=E9_Almeida_Maldonado?=, Jul 6, 2004.

  1. 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
    =?iso-8859-1?Q?Andr=E9_Almeida_Maldonado?=, Jul 6, 2004
    #1
    1. Advertising

  2. 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]


    "André Almeida Maldonado" <> wrote in message
    news:...
    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
    Ken Cox [Microsoft MVP], Jul 7, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Middleton

    Datagrid losing changes made in ItemDataBound

    Tony Middleton, Apr 22, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    294
    Tony Middleton
    Apr 22, 2004
  2. Dave

    ItemDataBound event of DataGrid

    Dave, Oct 7, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,926
    Scott Allen
    Oct 7, 2004
  3. RJN
    Replies:
    1
    Views:
    519
    Brent Humber
    Dec 24, 2006
  4. =?Utf-8?B?Sm9u?=

    DataGrid, ItemDataBound

    =?Utf-8?B?Sm9u?=, Jan 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    504
    Eliyahu Goldin
    Jan 13, 2005
  5. MattB
    Replies:
    4
    Views:
    21,193
Loading...

Share This Page