Link to home
Start Free TrialLog in
Avatar of aspnet-scotland
aspnet-scotlandFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

Avatar of krunal_shah
krunal_shah

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

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

Avatar of aspnet-scotland

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of NazoUK
NazoUK
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial