2011-05-02 2 views
0

TableCell Array를 정의하려고하는데 문제가 있습니다. 나는 다음과 같은 오류 얻을 사이트에서이 작업을 실행하려고하면TableCell을 정의하고 배열로 채우시겠습니까?

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 : 게시 전체 코드 루프를 위해하지 않습니다 의미에서

+0

조건은 'i

답변

1

조건. 세 요소의 배열을 사용하면 두 번만 실행됩니다. 이 때문에 배열의 세 번째 요소 (tc[2])는 null입니다. 할당 된 값을 얻지 못하기 때문에 null입니다. 두 번째 루프에서도 마찬가지입니다.

사용이 대신 :

for (int p = 0; p < tc.Length; p++) 

가 BTW : 귀하의 예제의 마지막 줄은 예외가 발생합니다 :

setTableRow(tc[2], tc[3], tr[1]); 

tc[3] 세 가지 항목의 배열에서 네 번째 요소입니다. 그러면 IndexOutOfRangeException이됩니다.

UPDATE : 당신이 당신의 전체 코드를 게시
후, 나는이 말을 할 수

다음 줄

가 원인 원인 IndexOutOfRangeException : 당신은 즉 9 개 요소와 함께 배열을 생성
tc[9].Text = Convert.ToString(goalItem["Deadline"]); 

setTableRow(tc[8], tc[9], tr[4]); 

0에서 까지 8의 인덱스. tc[9]은 9의 배열에서 10 번째 요소에 액세스하려고 시도합니다! 당신도 당신의 TableRow 배열과 같은 문제가, 또한

TableCell[] tc = new TableCell[10]; 

을, 그래서 당신은 그것의 크기를 증가해야합니다 :이 문제를 해결하려면, 10로 배열의 크기를 증가

TableRow[] tr = new TableRow[5]; 
+0

그건 내 원래의 루프, 그러나, 나는 오류 메시지가 나타납니다 : 인덱스 때마다 그것을 사용하여 배열의 경계를 벗어났습니다. –

+0

게시 한 코드로는 불가능합니다.이 경우 실제 코드와 게시 한 코드가 일치하지 않습니다. –

+0

지금 전체 코드를 게시했지만 여전히 인덱스가 범위를 벗어납니다. 문제를 찾을 수없는 것 같습니다. –

1

수정 나 내가 틀렸다면,하지만 당신은 배열을 만들 때 :

0,123,012 :

TableCell[] tc = new TableCell[3] 

을 당신은 요소가

tc [3]에 할당 할 수 없습니다.
왜 길이가 1 인 루프를 만드나요? 전체 길이 여야합니다.

+0

이것은 단지 오타였습니다. tc [3]가 아니라 tc [2]라고해야합니다. 또한 인덱스가 배열 오류 메시지의 범위를 벗어 났기 때문에 루프에 -1을 넣었습니다. :) –

관련 문제