2012-10-30 36 views
0

到目前爲止,我有以下代碼顯示列表中的列表項目(使用自定義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

如果你想從字面上建立細胞通過細胞的表,我相信過程是任何援助是這樣的:

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

我相信這是足以讓一個簡單的表列標題。

+0

嗨協助下,你的第一個例子看起來像去爲病房一次工作,我會加入CSS樣式該表後的方式。無論如何,我對於foreach循環內部「字段中的字符串字段」來自哪裏有點困惑?謝謝 –

+0

'new [] {「Title」,「Band」,「PeopleRecommended」,「Band2」}''會做到這一點,儘管您可以爲我使用它的兩個地方(列標題/列表字段名稱)有不同的地方。 – Rawling

+0

除了創建自定義Linq對象外,還可以使用[GetDataTable方法](http://msdn.microsoft.com/zh-cn/library/microsoft.sharepoint.splistitemcollection.getdatatable.aspx)返回一個DataTable可以綁定到一個GridView。 –

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循環外的表格。

更簡單和偉大工程,感謝所有的大加讚賞

相關問題