开发者

Convert DataRow to TableRow

Simple enough. Cant find a simple solution. Not sure if their is a simple solution? :/

I have a dataTable开发者_运维技巧. I essentially want to achieve this...

DataTable dt = new DataTable();
dataAdapter.Fill(dt);

TableRow tr = new TableRow();
tr = dt.Rows[0];

but i cant convert from DataRow to a TableRow!

Help!

Alex


A TableRow is a UI element, it is represents a row within a Table control). The DataRow is a data element, it represents a row of data within a DataTable. You can't convert between the two like this because they are simply different beasts.

If you're looking to work with the UI then you should bind the Table control to the DataTable. Then you can pull the individual TableRow objects.

What are you trying to do?


if your goal is to show on the UI the data that are within a dataTable(or any other datasource) why don't you use a repeater?

anyway you can't just convert the DataTableRow to TableRow but you have to do it yourself. have a look at the below code

private void GenerateTable()

    {

        DataTable dt = CreateDataTable();

        Table table = new Table();

        TableRow row = null;



        //Add the Headers

        row = new TableRow();

        for (int j = 0; j < dt.Columns.Count; j++)

        {

            TableHeaderCell headerCell = new TableHeaderCell();

            headerCell.Text = dt.Columns[j].ColumnName;

            row.Cells.Add(headerCell);

        }

        table.Rows.Add(row);



        //Add the Column values

        for (int i = 0; i < dt.Rows.Count; i++)

        {

            row = new TableRow();

            for (int j = 0; j < dt.Columns.Count; j++)

            {

                TableCell cell = new TableCell();

                cell.Text = dt.Rows[i][j].ToString();

                row.Cells.Add(cell);

            }

            // Add the TableRow to the Table

            table.Rows.Add(row);

        }

        // Add the the Table in the Form

        form1.Controls.Add(table);

    }

source:

http://geekswithblogs.net/dotNETvinz/archive/2009/06/24/fill-asp.net-table-with-data-from-datatable.aspx


you can use this

TableRow tr = new TableRow();

    tr .Cells(0).Text = dt.Rows[0][0];

because TabeRow have so many cell, you must have to specified cell. there is no method to add whole datarow in to TableRow both are Different.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜