以编程方式显示表中的列表项

时间:2012-10-30 10:31:31

标签: c# sharepoint-2010 foreach web-parts listitem

到目前为止,我有以下代码显示列表中的列表项(使用自定义Web部件属性获取列表URL和列表名称)。

显示列表项的代码:

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()));

  }

  }

  }

  }

以编程方式生成表以显示列表项的最简单方法是:

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

我之前没有做过以编程方式创建表格,所以我有点困惑。我已经开始使用该表的一些代码来让我思考,但还没有设法把它放在一起。

对此的任何帮助或者可能是指向良好教程的链接都将非常感谢

非常感谢,

2 个答案:

答案 0 :(得分:2)

如果你想逐个细胞地构建一个表,我相信这个程序是这样的:

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);

但是,您可以使用数据绑定控件更简单地执行此操作,例如GridView

例如,在您的情况下,我个人会做类似

的事情
// 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();

我认为这足以得到一个带有列标题的简单表格。

答案 1 :(得分:0)

找到了一种更快捷,更简单的方法:

//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>"));

而是使用使用for循环生成表的相当复杂的方法,我刚刚决定在foreach循环之外创建表,然后在foreach循环内创建表行,然后在外面关闭表foreach循环。

非常简单,效果很好,感谢所有人非常感谢