在彼此之下显示数据行 [英] Displaying datarows underneath each other

查看:161
本文介绍了在彼此之下显示数据行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何循环遍历一个数据表,使行显示整齐地在彼此的下方,逐行。

How can I loop through a datatable so that the rows display neatly underneath one another,row by row.

我尝试过的是以下内容,数据显示在一列中。

what I have tried is the following but like this all the data displays in one column.

foreach (DataRow row in myTopTenData.Rows)
            {
                foreach (DataColumn col in myTopTenData.Columns)
                {
                    Console.Write(row[col].ToString() + " ");
                    Console.WriteLine();
                }
            }


推荐答案

foreach (DataRow row in myTopTenData.Rows)
    {
        foreach (DataColumn col in myTopTenData.Columns)
            Console.Write(string.Format("{0, -10}", row[col].ToString()));

        Console.WriteLine();
     }

string.Format{0,-10} 将帮助您对齐列(左对齐使用负值,正确对齐为正值,当然10为任意值)。

string.Format"{0, -10}" will help you to align your columns (use negative values for a left alignment, positive for a right alignement, and of course 10 is an arbitrary value).

这篇关于在彼此之下显示数据行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆