ASP.NET C # Создать таблицу динамически не работает - PullRequest
0 голосов
/ 02 октября 2011

Я пытался сгенерировать таблицу с n числом строк.Привыкание к PHP делает все это худшим.Я попробовал следующий код:

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);

, но это не сработало!

1 Ответ

1 голос
/ 02 октября 2011

Вместо выполнения this.Controls.Add (table1) добавьте таблицу на страницу .aspx, а затем измените ее с помощью кода.

Еще лучше - используйте GridView с привязкой к данным.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...