2011-01-14 6 views
0

정말 고맙겠습니다. 이것은 내 문제입니다 :asp.net에서 동적 테이블 채우기?

제품 객체의 ArrayList가 있습니다. 각 제품에는 ID, 이름 및 공급자가 있습니다. arraylist를 반복하고이 값을 셀에 넣기위한 테이블을 만들면 제품에 여러 공급 업체가있을 수 있다는 문제가 발생합니다. 이 경우 ID와 이름은 동일하지만 arraylist의 공급 업체가 다릅니다. 지금까지 코드는 id와 name에 대해 빈 셀을 생성하고 다른 셀을 새 셀에 넣음으로써 이것을 처리합니다.

그러나 모든 공급 업체에 대해 새로운 행을 작성하는 것은 좋지 않습니다. 필자가 원하는 것은 제품에 여러 공급 업체가있는 경우 행의 동일한 셀에있는 모든 공급 업체가 제품 ID를 원한다는 것입니다.

string id = string.Empty; 
int count = 0; 
public void CreateResultTable(ArrayList result) 
{ 
    TableRow row; 
    TableCell cell; 

    if (result != null && result.Count > 0) 
    { 
     foreach (Item item in result) 
     { 

      if (count == 0) 
      { 
       row = new TableRow(); 
       id = item.id; 

       cell = new TableCell(); 
       cell.Text = item.id; 
       row.Cells.Add(cell); 

       cell = new TableCell(); 
       cell.Text = item.product; 
       row.Cells.Add(cell); 

       cell = new TableCell(); 

       ArrayList levList = item.suppliers; 
       if (levList != null) 
       { 

        string lev = string.Empty; 
        for (int i = 0; i < levList.Count; i++) 
        { 
         lev += levList[i]; 
        } 
        cell.Text = lev; 
        row.Cells.Add(cell); 

       } 
       else 
        cell.Text = string.Empty; 
        row.Cells.Add(cell); 

       count++; 
      } 
      else if (id != item.id) 
      { 

       row = new TableRow(); 
       id = item.id; 

       cell = new TableCell(); 
       cell.Text = item.id; 
       row.Cells.Add(cell); 

       cell = new TableCell(); 
       cell.Text = item.product; 
       row.Cells.Add(cell); 

       cell = new TableCell(); 

       ArrayList levList = item.suppliers; 
       if (levList != null) 
       { 

        string lev = string.Empty; 
        for (int i = 0; i < levList.Count; i++) 
        { 
         lev += levList[i]; 
        } 
        cell.Text = lev; 

       } 
       else 
        cell.Text = string.Empty; 
       row.Cells.Add(cell); 


      } 
      else 
      { 
       row = new TableRow(); 
       cell = new TableCell(); 
       cell.Text = string.Empty; 
       row.Cells.Add(cell); 

       cell = new TableCell(); 
       cell.Text = string.Empty; 
       row.Cells.Add(cell); 

       cell = new TableCell(); 
       ArrayList levList = item.suppliers; 
       if (levList != null) 
       { 
        string lev = string.Empty; 
        for (int i = 0; i < levList.Count; i++) 
        { 
         lev += levList[i]; 
        } 
        cell.Text = lev; 
        row.Cells.Add(cell); 
       } 
       else 
        cell.Text = string.Empty; 
       row.Cells.Add(cell); 
      } 

      SearchResultLev.Rows.Add(row); 



     } 


     SearchResultLev.Visible = true; 
     SearchResult.Visible = false; 
     NoSearchResult.Visible = false; 
    } 

    else 
    { 
     SearchResultLev.Visible = false; 
     SearchResult.Visible = false; 
     NoSearchResult.Visible = true; 
    } 

} 
+0

나는 u를 sql.Pls에서 그룹화 원인을 사용하여 공급자를 선택할 수 있다고 생각한다. – kbvishnu

+0

Harie - 데이터베이스에 대한 액세스 권한이 없으므로 코드에서 수행 할 수 있다면 정말 좋을 것이다. 불가능하다면 나는 데이터베이스에 접근해야한다. – Andy

답변

0

당신은 여기 Hashtable를 사용할 수 있습니다

 Hashtable htProductCell = new Hashtable(); 
     if (!htProductCell.Contains(item.product)) 
     { 
      //add a new row for the item 
      htProductCell.Add(item.product, cell); 
     } 
     else 
     { 
      TableCell cell = (TableCell)htProductCell[item.product]; 
      ArrayList levList = item.suppliers; 
      if (levList != null) 
      { 
       string lev = string.Empty; 
       for (int i = 0; i < levList.Count; i++) 
       { 
       lev += levList[i]; 
       } 
       cell.Text += lev; 
       row.Cells.Add(cell); 
      } 

    } 
1

대신에 테이블을 생성하는 코드를-뒤의 GridView를 사용합니다. 여기 GridView의 Item Template 안에있는 GridView와 Repeater를 사용하는 샘플을 보았습니다. repeater는 각 공급자에 대해 제한되지 않은 목록을 작성합니다.

마크 업 :

<asp:GridView ID='GridView1' runat='server' AutoGenerateColumns='false'> 
     <Columns> 
      <asp:BoundField HeaderText='Product ID' DataField='ID' /> 
      <asp:BoundField HeaderText='Name' DataField='Name' /> 
      <asp:TemplateField HeaderText='Suppliers'> 
       <ItemTemplate>      
        <asp:Repeater DataSource='<%# Bind("Suppliers") %>' runat="server" ID='Repeater1'> 
         <HeaderTemplate> 
          <ul> 
         </HeaderTemplate> 
         <ItemTemplate> 
          <li><%# Eval("Name") %></li> 
         </ItemTemplate> 
         <FooterTemplate> 
          </ul> 
         </FooterTemplate> 
        </asp:Repeater> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
</asp:GridView> 

그리고 데이터를 바인딩하는 코드, 내가 샘플 TestProduct 및 TestSuppliers을 사용했습니다

GridView1.DataSource = new List<TestProduct> 
     { 
      new TestProduct 
      { 
       Name = "Test", 
       ID = "1", 
       Suppliers = new List<TestSupplier> 
       { 
        new TestSupplier { Name="Supplier1" }, 
        new TestSupplier { Name = "Supplier2" }, 
        new TestSupplier { Name =" A very long supplier name"} 
       } 
      } 
     }; 

     GridView1.DataBind(); 

public class TestProduct 
    { 
     public String ID { get; set; } 
     public String Name { get; set; } 
     public List<TestSupplier> Suppliers { get; set; } 
    } 

public class TestSupplier { public String Name { get; set; }} 

샘플 (유형의 정의는 다음과 같습니다) 출력 : alt text