Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

If I am returning items from a SharePoint list is there anyway that I can store the values in an array or a list to use them in an ASP Repeater? I would have 4 values that would need to be stored (the List has 4 columns) and random numbers of records (rows) that would be returned. Is there any way to accomplish this?

            using (SPSite site = new SPSite("MYSITE"))
                    using (SPWeb oWebsiteRoot = site.OpenWeb())
                    {
                        SPList oList = oWebsiteRoot.Lists["SInfo"];
                        SPListItemCollection items = oList.Items;

                        foreach (SPListItem listitem in items)
                        {
                            //ADD 4 Items to an array or List
                        }
                    }
share|improve this question

3 Answers 3

up vote 5 down vote accepted

Try:

Repeater.DataSource = oList.Items.GetDataTable();
Repeater.DataBind();

Should do the trick.

share|improve this answer
    
I tried that but when I try to access my page I get an error DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Market'. when I just used the line Market: <%# Eval("Market") %> <br /> Why cant I reference my column market? –  atrljoe Aug 10 '11 at 18:12
    
Did you inspect the table to see what columns it contains? You may also want to use GetListItems with a query to only return the columns you want, especially if you have a lot of columns on the list and/or a lot of items. –  Rob D'Oria Aug 10 '11 at 18:26
1  
I know it's been almost a year, but it helped me today! +1 from me. To display the desired column, I used the following: <%# DataBinder.Eval(Container.DataItem, "column-name") %> –  utkai Jul 10 '12 at 9:23
    
+1 from me, it helped me! –  Anuja Nov 26 '12 at 10:45

It's better to pass entity object to your repeater exemple :

public class MyEntity
{
   public string Title { get; set; }
   public string Description { get; set; }
}

System.Collections.Generic.List<MyEntity> MyRepeaterBind(SPListItemCollection collection)
{
   List<MyEntity> items = new List<MyEntity>();
   foreach (SPListItem item in collection)
   {

      items.Add(new MyEntity
      {
         Title = Convert.ToString(item["your title column here"]),
         Description = Convert.ToString(item["your description column here"])
      });
   }
   YourRepeater.DataSource = items;
   YourRepeater.DataBind();
}

In your asp just use <%# Eval("Title") %> <%# Eval("Decription") %>

share|improve this answer

you can not use Eval("Market") because SPListItem does not have any property called Market.

what you can do is something like that:

<%# ((SPListItem)Container.DataItem)["Market"] %>

You will just need to import the SharePoint assembly in your page :

<%@ Import Namespace="Microsoft.SharePoint" %>   
<%@ Assembly Name=""Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"" %> 

EDIT : it applies only if you had bind the SPListItemCollection directly without using the GetDataTable() method

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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