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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
NazoUKCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.