Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式显示表中的列表项_C#_Sharepoint 2010_Foreach_Web Parts_Listitem - Fatal编程技术网

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

C# 以编程方式显示表中的列表项,c#,sharepoint-2010,foreach,web-parts,listitem,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.ListUr

到目前为止,我有以下代码显示列表中的列表项(使用自定义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()));

  }

  }

  }

  }
我以前没有用编程方式创建过很多表,所以我有点困惑。我已经开始为表编写一些代码来让我思考,但还没有把它们组合起来

如果您能在这方面提供任何帮助,或者提供一个好教程的链接,我们将不胜感激


非常感谢,

如果您真的想一个单元格一个单元格地构建一个表格,我相信这个过程是这样的:

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);
但是,您可以通过数据绑定控件(如

例如,在你的情况下,我个人会做

// 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();
//和以前一样,直到你有了你的收藏。
//从列表项中创建简单的匿名对象。
var items=listItemColl.Cast()
.选择(li=>new{
Chambers=li[“Title”]。ToString(),
Band=li[“Band”]。ToString(),
PeopleRecommendated=li[“PeopleRecommendated”]。ToString(),
Band2=li[“Band2”].ToString()});
//将对象绑定到GridView。
var gridView=new gridView();
plhdirlinkings.Controls.Add(gridView);
gridView.DataSource=项目;
gridView.DataBind();

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

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

//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>"));
//创建表和表侦听器
plhdirlinkings.Controls.Add(新的文字控件(“+”Chambers“+”+
“乐队”“+”+“人
推荐的乐队
+ ""));
//获取列表中的所有列表项并显示它们
foreach(listItemColl中的SPListItem listItem)
{
//获取listitem值
chambers=listItem[“Title”].ToString();
band=listItem[“band”].ToString();
PeopleRecommendated=listItem[“PeopleRecommendated”].ToString();
band2=listItem[“band2”].ToString();
//生成表内容
plhdirlinkings.Controls.Add(新的文字控件(“+chambers+
“+乐队+”+推荐人数+
“+band2+”);
}
//结束会议
plhdirlinkings.Controls.Add(新的LiteralControl(“”);
我决定在foreach循环外部创建表,然后在foreach循环内部创建表行,然后在foreach循环外部关闭表,而不是使用相当复杂的方法使用for循环生成表


简单多了,效果很好,感谢大家的帮助,非常感谢

您好,您的第一个示例看起来就像是在病房之后一样。一旦开始工作,我将向该表添加css样式。在任何情况下,我都不太清楚foreach循环中的“字段中的字符串字段”是从哪里来的?感谢
new[]{“Title”,“Band”,“PeopleRecommended”,“Band2”}
可以做到这一点,尽管我使用它的两个地方(列标题/列表字段名)可以有不同的名称。除了创建自定义Linq对象外,还可以使用返回可以绑定到GridView的数据表。@Rich我个人不喜欢数据表,但既然我们试图在一个表中显示数据,我不明白为什么不能!
//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>"));