Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 24th, 2006, 07:48 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default Item Data Bound Problem

Hi All,

I have a Sub to handle ItemDataBound in a DataGrid. The sub totals certain columns in the data grid to display in the footer. It also formats certain columns for Item and AlternatingItem. The problem I'm having is in the formatting the Item and AlternatingItem section. You can see in the code that all I'm trying to do is to center the data in those columns.

The problem is that it works on every row except the first one. It will not go into the For loop on the first pass. Also, in debugging the sub, on the first pass the value for grdIneventory.Items.Count = 0, so it doesn't run the For loop.

How can I resolve this problem?

Thank you for your response. Much appreciated.

Richard

        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim i As Integer = 0
            Dim n As Integer
            'Will not go into this loop on the first pass.
            For i = 0 To grdInventory.Items.Count - 1
                For n = 4 To grdInventory.Items.Item(i).Cells.Count - 1
                    e.Item.Cells(n).Attributes.Add("align", "center")
                Next
            Next
        ElseIf e.Item.ItemType = ListItemType.Footer Then
            Dim i As Integer
            Dim n As Integer
            Dim y As Integer
            For i = 0 To grdInventory.Items.Count - 1
                n = grdInventory.Items.Item(i).Cells.Count - 1
            Next
            Dim x(n) As Integer
            For i = 0 To grdInventory.Items.Count - 1
                For n = 4 To grdInventory.Items.Item(i).Cells.Count - 1
                    x(n) += CInt(grdInventory.Items.Item(i).Cells(n).Text)
                    e.Item.Cells(n).Text = Format(x(n), "##,###")
                    e.Item.Cells(n).Attributes.Add("align", "center")
                    If Len(e.Item.Cells(n).Text) = 0 Then
                        e.Item.Cells(n).Text = "0"
                    End If
                Next
            Next
            For i = 0 To UBound(x)
                y += x(i)
            Next
            e.Item.Cells(0).Text = "Grand Total"
            e.Item.Cells(0).Attributes.Add("align", "right")
            e.Item.Cells(1).Text = Format(y, "##,###")
            e.Item.Cells(2).Text = "Sub Totals"
            e.Item.Cells(2).Attributes.Add("align", "right")
        End If



 
Old January 25th, 2006, 01:23 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

   Since you are in the ItemDataBound event, the "grdInventory.Items.Count - 1" will always = 0 on the first pass. The ItemDataBound fires for each row as it is being bound, essentially looping through the rows for you. So, you can get rid of the first(Outer Loop), and just loop through the cells in each datagriditem. So, this is all the code you will need:
        Dim n as integer
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

            For n = 4 To e.Item.Cells.Count - 1
                e.Item.Cells(n).Attributes.Add("align", "center")
            Next

        End If


The code will loop through each cell or each datagriditem(row) as it is being bound to the grid.

Jim


 
Old January 25th, 2006, 05:27 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Hi Jim,

Thanks for taking the time to respond. That did the trick. Very much appreciated. Thank you.

Richard

 
Old January 25th, 2006, 05:39 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Cool.. glad you got it working..
Jim






Similar Threads
Thread Thread Starter Forum Replies Last Post
Prefer Not To Use Data-Bound Controls... soundchaser59 ASP.NET 2.0 Basics 1 July 9th, 2007 09:13 PM
retrieving a value from data bound to a repeater beeyule General .NET 0 September 8th, 2005 11:59 AM
Need help with bound data kcrompt2 Classic ASP Professional 0 February 9th, 2005 12:37 AM
Data Bound ComboBox joblot VB How-To 0 May 5th, 2004 12:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.