简体   繁体   中英

Displaying list items in a table programatically

I have the following code so far which displays list items from a list (using custom web parts properties to obtain the list url and list name).

Code to display list item:

if(this.WebPart.ListUrl != null && this.WebPart.ListName != null && 
this.WebPart.AwardYear != null)
{


//getting custom properties values
string listURL = this.WebPart.ListUrl.ToString();
string listName = this.WebPart.ListName.ToString();
string awardYear = this.WebPart.AwardYear.ToString();


using (SPSite site = new SPSite(listURL))
{

using (SPWeb web = site.OpenWeb())
{

try
{


 SPList list = web.Lists[listName]; //name of the list


 //CAML query to filter list items by year and then order by year in descending order
 SPQuery awardsYear = new SPQuery();
 awardsYear.Query = @"<Where><Eq><FieldRef Name='Year'/><Value Type='Text'>" + 
 awardYear + @"</Value></Eq></Where>" + "<OrderBy><FieldRef Name='Year' 
 Ascending='False' /></OrderBy>";


 SPListItemCollection listItemColl = list.GetItems(awardsYear);


 //code for generating the table goes here EXPERIMENTAL
 Table table1 = new Table();
 TableRow tableRow = new TableRow();
 TableCell tableCell = new TableCell();



 int numberOfColumns = 4; //number of columns for the chambers table
 for (int x = 0; x < numberOfColumns; x++)
 {

 //Table columns created here need to be added somehow to the table above


 }



 //getting all the list items in the list and displaying them
 foreach (SPListItem listItem in listItemColl)
 {

  //For each of the list items create the table rows






  //The below needs to be put into a table generated programatically
  chambers = listItem["Title"].ToString();
  band = listItem["Band"].ToString();
  peopleRecommended = listItem["PeopleRecommended"].ToString();
  band2 = listItem["Band2"].ToString();

  //placeholders used to display the results
  plhDirRankings.Controls.Add(new LiteralControl("Chambers: " + chambers + "<br/>"));
  plhDirRankings.Controls.Add(new LiteralControl("Band: " + band + "<br/>"));
  plhDirRankings.Controls.Add(new LiteralControl("People Recommended: " + 
  peopleRecommended + "<br/>"));
  plhDirRankings.Controls.Add(new LiteralControl("Band: " + band2 + "<br/>"));


  }



  }

  catch (Exception err)
  {

  plhDirRankings.Controls.Add(new LiteralControl(err.ToString()));

  }

  }

  }

  }

what's the easiest way to generate a generate a table programmatically to display the list items as follows:

Chambers | Band  | PeopleRecommended | Band2 
--------------------------------------------
item1    | item1 | item1             | item1
item2    | item2 | item2             | item2

I haven't done much with creating tables programatically before so I am a little confused. I have started some of the code for the table to get me thinking but haven't managed to put it together.

Any assistance on this or perhaps a link to a good tutorial would be greatly appreciated

Many Thanks,

If you want to literally build a table cell-by-cell, I believe the procedure is something like:

Table table = new Table();
TableRow headerRow = new TableRow();
foreach(string field in fields)
{
    TableCell headerCell = new TableCell();
    headerCell.Text = field;
    headerRow.Controls.Add(headerCell);
}
foreach(SPListItem li in listItemColl)
{
    TableRow dataRow = new TableRow();
    foreach(string field in fields)
    {
        TableCell dataCell = new TableCell();
        dataCell.Text = li[field].ToString();
        dataRow.Controls.Add(dataCell);
    }
}
plhDirRankings.Controls.Add(table);

However, you can do this much more simply with a databound control, such as a GridView .

For example, in your case, I'd personally do something like

// As before until you have your collection.

// Create simple anonymous objects out of your list items.
var items = listItemColl.Cast<SPListItem>()
    .Select(li => new {
        Chambers = li["Title"].ToString(),
        Band = li["Band"].ToString(),
        PeopleRecommended = li["PeopleRecommended"].ToString(),
        Band2 = li["Band2"].ToString()});

// Bind objects to a GridView.
var gridView = new GridView();
plhDirRankings.Controls.Add(gridView);
gridView.DataSource = items;
gridView.DataBind();

which I believe is enough to get a simple table with column headings.

Have found a quicker and simpler way to do this:

//creating the table and the table hearers
plhDirRankings.Controls.Add(new LiteralControl("<table><tr><td width='170' 
valign='top'>" + "<strong>Chambers</strong>" + "</td><td width='180' valign='top'>" + 
"<strong>Band</strong>" + "</td><td width='180' valign='top'>" + "<strong>People 
Recommended</strong>" + "</td><td width='145' valign='top'>" + "<strong>Band</strong>" 
+ "</td></tr>"));


 //getting all the list items in the list and displaying them
 foreach (SPListItem listItem in listItemColl)
 {


  //getting listitem values
  chambers = listItem["Title"].ToString();
  band = listItem["Band"].ToString();
  peopleRecommended = listItem["PeopleRecommended"].ToString();
  band2 = listItem["Band2"].ToString();

  //Generating the table content 
  plhDirRankings.Controls.Add(new LiteralControl("<tr><td valign='top'>" + chambers + 
  "</td><td valign='top'>" + band + "</td><td valign='top'>" + peopleRecommended + 
  "</td><td valign='top'>" + band2 + "</td></tr valign='top'>"));



   }

   //closing the table
   plhDirRankings.Controls.Add(new LiteralControl("</table>"));

Rather using the rather complicated way of generating a table using a for loop, I have just decided to create the table outside of the foreach loop, then have the table rows created inside of the foreach loop, and then closing the table outside of the foreach loop.

Much simpler and works great, thanks to all for the assistance much appreciated

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM