2011-10-02 3 views
0

테이블을 n 개의 행으로 생성하려고했습니다. PHP에 익숙해지면이 모든 것이 최악의 결과를 낳습니다. 다음 코드를 시도 :ASP.NET C# 테이블을 동적으로 생성 할 수 없습니다.

using System.Data; 

// Create a DataTable instance 
DataTable dTbl = new DataTable("myDynamicTable"); 

// Create a DataColumn instances 

DataColumn dValue = new DataColumn(); 
DataColumn dMember = new DataColumn(); 

dValue.ColumnName = "Id"; 
dValue.DataType = Type.GetType("System.Int32"); 

dMember.ColumnName = "Name"; 
dMember.DataType = Type.GetType("System.String"); 

// Add these DataColumns into the DataTable 

dTbl.Columns.Add(dValue); 

dTbl.Columns.Add(dMember); 
DataRow myrow = dTbl.NewRow(); 

myrow["Id"] = 1; 
myrow["Name"] = "Tux"; 

// Add the row into the table 

dTbl.Rows.Add(myrow); 

아무 것도 표시되지 않았습니다. 왜 그런가? 필요한 것은 3 개의 열과 n 개의 행이있는 테이블을 표시하는 것입니다. 이 행 수는 특정 조건을 만족하는 데이터베이스의 레코드 수에 따라 달라집니다.

나는 또한이 시도 :

HtmlTable table1 = new HtmlTable(); 

// Set the table's formatting-related properties. 
table1.Border = 1; 
table1.CellPadding = 3; 
table1.CellSpacing = 3; 
table1.BorderColor = "red"; 

// Start adding content to the table. 
HtmlTableRow row; 
HtmlTableCell cell; 
for (int i = 1; i <= 5; i++) 
{ 
    // Create a new row and set its background color. 
    row = new HtmlTableRow(); 
    row.BgColor = (i % 2 == 0 ? "lightyellow" : "lightcyan"); 

    for (int j = 1; j <= 4; j++) 
    { 
     // Create a cell and set its text. 
     cell = new HtmlTableCell(); 
     cell.InnerHtml = "Row: " + i.ToString() + 
      "<br>Cell: " + j.ToString(); 

     // Add the cell to the current row. 
     row.Cells.Add(cell); 
    } 

    // Add the row to the table. 
    table1.Rows.Add(row); 
} 

// Add the table to the page. 
this.Controls.Add(table1); 

했지만 작동하지 않았다!

+0

"dTbl"을 어떻게 사용했는지 또는 두 번째 예에서 어떤 일이 발생했는지에 대해서는 아무 것도 말하지 않았습니다. –

+0

이 코드가 삽입 한 페이지의 이벤트 처리기는 무엇입니까? –

답변

1

"this.Controls.Add (table1)"을 수행하는 대신 테이블을 .aspx 페이지에 추가 한 다음 코드를 통해 수정하십시오.

데이터 바인딩 된 GridView를 사용하는 것이 더 좋습니다.

+0

힌트를 주셔서 감사합니다 ... 그것을 밖으로 시도하지만 나는 이것이 그것이라고 생각합니다 :) –

관련 문제