16

Im setting the the datasource with the following code:

    protected void Page_Load(object sender, EventArgs e)
    {
        var vacancies = from v in db.Vacancies
                    join c in db.Customers on v.CustomerID equals c.CustomerID
                    join cp in db.CustomerPortals on c.CustomerID equals cp.CustomerID
                    where cp.PortalID == Master.Portal.ID
                    select new
                    {
                        Title = v.Title,
                        Internship = (v.ContractID == 6),
                        Hours = v.Hours,
                        City = v.Customer.City.Name,
                        Degree = v.Degree.Title,
                        Contract = v.Contract.Title,
                        CustomerID = v.CustomerID
                    };
        rVacancies.ItemDataBound += new RepeaterItemEventHandler(rVacancies_ItemDataBound);
        rVacancies.DataSource = vacancies;
        rVacancies.DataBind();
    }

Now i want to know how i can access 1 of the columns (like CustomerID) from the ItemDataBound event.

    void rVacancies_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
         // This doesnt seem to work, row would be null even though e.Item.DataItem has a value.
         DataRow row = (DataRow)e.Item.DataItem;
    }

I have figured out that e.Item.DataItem contains all the fields from my query and the type of e.Item.DataItem is

f__AnonymousType8<string,bool,byte,string,string,string,long>

3 Answers 3

37

Finally found it, was as simple as the following:

long customerID = long.Parse(DataBinder.Eval(e.Item.DataItem, "CustomerID").ToString());
0
37

This .Net 4.0 approach is also very cool indeed!

public void PersonDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        dynamic person = e.Item.DataItem as dynamic;

        string name = person.Name;
        int age = person.Age;
    }
}

All Credit To: http://www.kristofclaes.be/blog/2010/08/12/anonymous-types-and-the-itemdatabound-event/

Because the page now shows an Error 404, here is the page from the Wayback Machine: Anonymous types and the ItemDataBound event (Archived version)

1
-2

You're not binding a datarow to your control (you're binding an anonymous type), so you should not cast DataItem to DataRow.

Try to get your row's data as :

var dataItem = e.Item.DataItem;
// For example get your CustomerID as you defined at your anonymous type :
string customerId = dataItem.CustomerID;
1
  • 9
    So I'm two years late to the game on this one, but no one else has noticed this won't compile? "var" still tries to infer the data type at design time and since it's an anonymous type it can't do this. This code should give you the error "Cannot resolve symbol 'CustomerID' on the second line. RobD's method is correct assuming .NET 4.0.
    – Scott
    Nov 3, 2011 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.