How do I use the ItemDataBound event using C#?

Hi,

I have the attached ItemDataBound event attached to my asp.net listview control. I also have the attached "Traders" class that populates a List that I want to bind to my listview control. Unfortunately I receive the error "Operator '==' cannot be applied to operands of type 'System.Web.UI.WebControls.ListViewItemType' and 'System.Web.UI.WebControls.ListItemType'" on line 3 and "'System.Web.UI.WebControls.ListViewItem' does not contain a definition for 'DataItem' and no extension method 'DataItem' accepting a first argument of type 'System.Web.UI.WebControls.ListViewItem' could be found (are you missing a using directive or an assembly reference?)" on line 8?

Thanks.
protected void lvTrustAccounts_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ContentRating myRating = e.Item.FindControl("Rating1") as ContentRating;
            if (myRating != null)
            {
                Traders myBook = e.Item.DataItem as Traders;
                if (myBook != null)
                {
                    myRating.ItemId = myBook.Id;
                    myRating.DataSource = myBook.Rating;
                }
            }
        }
    }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class Traders
{
    public string Name { get; set; }
    public int[] Rating { get; set; }
    public int Id { get; set; }

    public List<Traders> GetList()
    {
        List<Traders> tempList = new List<Traders>();
        for (int i = 0; i < 5; i++)
        {
            Traders myTraders = new Traders();
            myTraders.Id = i;
            myTraders.Name = "Traders " + i.ToString();
            myTraders.Rating = new int[] { 0, 0, 0, 0, 0 };
            myTraders.Rating[i] = i + 1;
            tempList.Add(myTraders);
        }
        return tempList;
    }
}

Open in new window

aspnet-scotlandAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
NazoUKConnect With a Mentor Commented:
ListView items use a different enumeration from other controls: ListViewItemType it has 3 values: DataItem, EmptyItem, InsertItem. Visual Studio should show you this in intellisense when you type.

To get at the dataitem, you'll first need to cast e.Item to type ListViewDataItem then cast the DataItem of this variable to whatever your dataType is that you bound to the listview.

Something like:
ListViewDataItem item = (ListViewDataItem)e.Item;
MyDataClass data = (MyDataClass)item.DataItem;
myRating.ItemId = data.ItemId;

and so on
0
 
krunal_shahCommented:
try like this,
protected void lvTrustAccounts_ItemDataBound(object sender, ListViewItemEventArgs e) 
    { 
        if (e.Item.ItemType == ListItemType.DataItem || e.Item.ItemType == ListItemType.AlternatingItem) 
        { 
            ContentRating myRating = e.Item.FindControl("Rating1") as ContentRating; 
            if (myRating != null) 
            { 
               ListViewDataItem dataItem = (ListViewDataItem)e.Item
               //do your code on thi dataItem
            } 
        } 
    } 
 

Open in new window

0
 
krunal_shahCommented:
correction,
check this also,
http://www.codeproject.com/KB/webforms/listviewdatabind.aspx
protected void lvTrustAccounts_ItemDataBound(object sender, ListViewItemEventArgs e)  
    {  
        if (e.Item.ItemType == ListItemType.DataItem )  
        {  
            ContentRating myRating = e.Item.FindControl("Rating1") as ContentRating;  
            if (myRating != null)  
            {  
               ListViewDataItem dataItem = (ListViewDataItem)e.Item 
               //do your code on thi dataItem 
            }  
        }  
    }  
 

Open in new window

0
 
aspnet-scotlandAuthor Commented:
I'm not too sure if I understand. I still get errors when I attempt the attached...

My code still doesn't like the "==" operator and still doesn't recognise DataItem?


protected void lvTrustAccounts_ItemDataBound(object sender, ListViewItemEventArgs e)   
    {   
        if (e.Item.ItemType == ListItemType.DataItem )   
        {   
            ContentRating myRating = e.Item.FindControl("Rating1") as ContentRating;   
            if (myRating != null)   
            {   
               ListViewDataItem dataItem = (ListViewDataItem)e.Item;  
               if (dataItem != null)
               {
                   myRating.ItemId = dataItem.Id;
                   myRating.DataSource = dataItem.Rating;
               }  
            }   
        }   
    }

Open in new window

0
All Courses

From novice to tech pro — start learning today.