我想定义一个TableCell数组,但有一些问题。 我的代码如下使用数组定义和填充TableCells?
public partial class ShowGoalsUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
SPWeb _web = SPContext.Current.Web;
SPList objGoalsList = _web.Lists["Goals"];
SPListItemCollection goalsItems = getItemsByQueryID(objGoalsList);
foreach (SPItem goalItem in goalsItems)
{
goalTitle.Text = "<h3>" + Convert.ToString(goalItem["Title"]) + "</h3>";
txtDescription.Text = Convert.ToString(goalItem["Description"]);
TableCell[] tc = new TableCell[9];
for (int i = 0; i < tc.Length; i++)
{
tc[i] = new TableCell();
}
tc[0].Text = "Status:";
setStatus(goalItem, tc[1], _web);
tc[2].Text = "Owner:";
SPFieldLookupValue thisOwner = getOwner(goalItem);
tc[3].Text = thisOwner.LookupValue;
tc[4].Text = "Perspective:";
SPFieldLookupValue thisPerspective = new SPFieldLookupValue(goalItem["Perspective"].ToString());
tc[5].Text = thisPerspective.LookupValue;
tc[6].Text = "Parent:";
SPFieldLookupValue thisParent = new SPFieldLookupValue(goalItem["Parent"].ToString());
tc[7].Text = thisParent.LookupValue;
tc[8].Text = "Deadline:";
tc[9].Text = Convert.ToString(goalItem["Deadline"]);
TableRow[] tr = new TableRow[4];
for (int p = 0; p < tr.Length; p++)
{
tr[p] = new TableRow();
}
setTableRow(tc[0], tc[1], tr[0]);
setTableRow(tc[2], tc[3], tr[1]);
setTableRow(tc[4], tc[5], tr[2]);
setTableRow(tc[6], tc[7], tr[3]);
setTableRow(tc[8], tc[9], tr[4]);
addToTable(tr);
}
}
SPListItemCollection getItemsByQueryID(SPList list)
{
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Text'>" + Request.QueryString["gid"] + "</Value></Eq></Where>";
SPListItemCollection items = list.GetItems(query);
return items;
}
void setStatus(SPItem item, TableCell status, SPWeb web)
{
string statusTest = Convert.ToString(item["Status"]);
switch (statusTest)
{
case "Approved":
status.Text = "<img src='" + web.Site.Url + "/_layouts/images/kpidefaultlarge-0.gif' height='10' width='10' />";
break;
case "Pending":
status.Text = "<img src='" + web.Site.Url + "/_layouts/images/kpidefaultlarge-1.gif' height='10' width='10' />";
break;
case "Declined":
status.Text = "<img src='" + web.Site.Url + "/_layouts/images/kpidefaultlarge-2.gif' height='10' width='10' />";
break;
}
}
SPFieldLookupValue getOwner(SPItem item)
{
SPFieldLookupValue getThisOwner = new SPFieldLookupValue(item["Owner"].ToString());
return getThisOwner;
}
void setTableRow(TableCell add1, TableCell add2, TableRow row)
{
row.Cells.Add(add1);
row.Cells.Add(add2);
}
void addToTable(TableRow[] tableRow)
{
for (int i = 0; i < tableRow.Length; i++)
{
infoTable.Rows.Add(tableRow[i]);
}
}
}
当我尝试到现场我收到以下错误上运行此:
Index was outside the bounds of the array.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.
我在做什么错?有没有更好的方法来做到这一点? 任何意见将有帮助!
编辑:TC [3]变更为TC [2] EDIT2:发布完整的代码
条件应该是'我
2011-05-02 10:42:24