tags:

views:

1587

answers:

9

I'm trying to display the ListItems in a gridview.

Please help me in finding a way to access the list items.

using (SPSite site = new SPSite("http://mysitehere......"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["TestList"];
        .......
        .......
    }
}

Please help me in accessing the list item values.

+1  A: 

Well, I haven't done any sharepoint work, but I believe you can use:

foreach (SPListItem item in list.Items)

or

foreach (SPListItem item in list.GetItems(view)) // or query

or access by index or guid:

SPListItem item = list.Items[10];
SPListItem item = list.Items[guid];
Jon Skeet
The is correct, however there's a limitation in SharePoint's architecture when using SPList.Items - it causes all items to retrieved from the content database on each call and then the enumerator is returned.The recommendation is to assing list.Items to a variable and then loop on the variable, soforeach (SPListItem item in list.Items)becomesSPListItemCollection listItems = list.Items;foreach (SPListItem item in listItems)
dariom
@dariom: There's no difference in that code. A foreach loop will only execute the initializing part once.
Jon Skeet
+3  A: 

Use SPDataSource to display list items in a grid view, i.e. create and configure an SPDataSource object and bind it to a SPGridView control.

Lars Fastrup
+3  A: 

The below code should do the trick, get the full article from msdn here

using (SPSite site = new SPSite("http://mysitehere......"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["TestList"];
        SPListItemCollection collListItems = list.Items;
        foreach (SPListItem oListItem in collListItems)
        {
           string value = SPEncode.HtmlEncode(oListItem["Field1_Name"]);
           // do something with value
         }
    }
}
Binoj Antony
+3  A: 
SharePoint Newbie
+1  A: 

The SPQuery approach as suggested by unknown(yahoo) is the way to go, however you should remember that the default rowlimit of SPQuery is 100:

try
        {
            using (SPSite site = new SPSite(theURL))
            {
                SPWeb web = site.OpenWeb();
                SPList list = web.Lists["TheNameOfTheList"];

                //search each ListItem where the date is after 1/1 2009
                SPQuery query = new SPQuery();

                //the default is 100, which is less then the expected max
                query.RowLimit = 500;

                // The U2U CAML Query builder rules :-)
                query.Query = "<Where><Gt><FieldRef Name='StartDate' /><Value Type='DateTime'>";
                query.Query += "2009-01-01T00:00:00Z</Value></Gt></Where>";
                SPListItemCollection resultset = list.GetItems(query);
                foreach (SPListItem item in resultset)
                {
                    // do something  
                }
            }
        }
Kasper
+1  A: 

Hi

Thank you for your replies.I was able to access the list item values with your help.But now i am not able to display the images from the custom list in datatable.

Can you help me in this issue.

Steve
+1  A: 

If you´re using .NET 3.5 I would recommend using the linq datasource together with the spgridview to bind splistitems to a gridview. Saves you a lot of time. I have an example of this on my blog. To display images you have to get the url of the image. Here is an example of that:

SPListItem item = GetItem();
string imagefieldhtml = item["NameOfImageField"].ToString();
ImageFieldValue imagefield = new ImageFieldValue(imagefieldhtml);

var url = imagefield.ImageUrl;

The url variable will now hold the image url.

Johan Leino
+1  A: 

For displaying items in a gridview (if you don't want to use SPGridView), I would use the GetDataTable method, instead of using a foreach loop as many of the answers do.

if (items.Count > 0)
{
    DataTable dt = items.GetDataTable();
    GridView1.DataSource = dt;
    GridView1.DataBind();
}
Rich Bennema
A: 

I have a SPList which has three fields: Image that holds Url of the publishing image and ImageDescription that holds a single line of text for description and a checkbox field called ImageChecked(yes/NO)

The probelm is if the checkbox is selected, it should display the image and the ImageDescription on the aspx page. Ideally there is only one item in the list with checkbox yes.

I m using SPQuery to query the list.

Here's how I did it. However its not giving me the result I wanted. I created a usercontrol1.ascx that has

//usercontrol1.ascx.cs has got the code below in page load event.

//Get the list

SPList HomepageImage = currentWeb.Lists["My Images"];

//Create a query

SPQuery query = new SPQuery();

query.Query =string.Format("1");

//Get the list items

SPListItemCollection collListItems = My Images.GetItems(query);

string sUrl = "";

foreach (SPListItem Listitem in collListItems)

{

Microsoft.SharePoint.Publishing.Fields.ImageFieldValue h = (Microsoft.SharePoint.Publishing.Fields.ImageFieldValue)Listitem["Image"];

//Image control

testImage.ImageUrl = h.ImageUrl;

testImage.ImageUrl = sUrl;

//Label control

testLabel.Text = Listitem["ImageDescHD"].ToString();

break;

}

 

Any thoughts to achieve this? Thanks.

sspb485