Laden...

DataTable to string

Erstellt von jaensen vor 14 Jahren Letzter Beitrag vor 14 Jahren 3.834 Views
jaensen Themenstarter:in
2.760 Beiträge seit 2006
vor 14 Jahren
DataTable to string

Beschreibung:

Mit diesem Snippet kann man eine DataTable als Text ausgeben.


        public static string FormatDataTable(DataTable table, bool header)
        {
            StringBuilder sb = new StringBuilder();

            int[] columnLengths = new int[table.Columns.Count];

            // First determine the maximum length of each column.
            // If the header should be considered we start with the header lengths
            // as initial values.
            GetMaxColumnlength(table, header, columnLengths);

            // Now we have the maximum column length of all rows and can build the output
            if (header)
            {
                for (int i = 0; i < table.Columns.Count; i++)
                {
                    DataColumn column = table.Columns[i];
                    if (column.Caption != null)
                        sb.Append(column.Caption.PadRight(columnLengths[i]));
                    else
                        sb.Append("".PadRight(columnLengths[i], ' '));

                    sb.Append("|");
                }
                sb.AppendLine();
                foreach (int length in columnLengths)
                {
                    sb.Append("".PadRight(length, '-'));
                    sb.Append("+");
                }
                sb.AppendLine();
            }
            foreach (DataRow row in table.Rows)
            {
                for (int i = 0; i < row.ItemArray.Length; i++)
                {
                    object obj = row.ItemArray[i];

                    if (obj != null)
                        sb.Append(obj.ToString().PadRight(columnLengths[i]));
                    else
                        sb.Append("".PadRight(columnLengths[i], ' '));

                    sb.Append("|");
                }
                sb.AppendLine();
            }


            return sb.ToString();
        }

        /// <summary>
        /// Determines the max. column length for every row and column.
        /// </summary>
        /// <param name="table"></param>
        /// <param name="header"></param>
        /// <param name="columnLengths"></param>
        private static void GetMaxColumnlength(DataTable table, bool header, int[] columnLengths)
        {
            if (header)
            {
                for (int i = 0; i < table.Columns.Count; i++)
                {
                    DataColumn column = table.Columns[i];
                    if (!string.IsNullOrEmpty(column.Caption))
                        columnLengths[i] = column.Caption.Length;

                }
            }
            foreach (DataRow row in table.Rows)
            {
                for (int i = 0; i < row.ItemArray.Length; i++)
                {
                    int length = row.ItemArray[i] == null ? 0 : row.ItemArray[i].ToString().Length;

                    if (length > columnLengths[i])
                        columnLengths[i] = length;
                }
            }
        }

Schaut dann so aus:

[pre]
Name                       |Creation           |Modified           |Rights|Size|
---------------------------+-------------------+-------------------+------+----+
$Recycle.Bin               |02.11.2006 12:17:19|17.06.2007 21:42:25|      |    |
Boot                       |18.06.2007 07:24:42|18.06.2007 07:24:44|      |    |
Documents and Settings     |02.11.2006 13:59:44|02.11.2006 13:59:44|      |    |
Dokumente und Einstellungen|17.06.2007 21:38:45|17.06.2007 21:38:45|      |    |
fsc.tmp                    |17.06.2007 21:44:55|17.06.2007 21:48:00|      |    |
Program Files              |02.11.2006 12:18:33|07.05.2009 15:32:43|      |    |
ProgramData                |02.11.2006 12:18:33|07.05.2009 14:21:24|      |    |
Programme                  |17.06.2007 21:38:45|17.06.2007 21:38:45|      |    |
System Volume Information  |17.06.2007 21:26:04|07.05.2009 14:34:06|      |    |
Users                      |02.11.2006 12:18:33|17.06.2007 21:41:59|      |    |
Windows                    |02.11.2006 12:18:34|05.05.2009 19:45:21|      |    |
                           |02.11.2006 11:23:09|18.09.2006 23:43:36|      |    |
                           |18.06.2007 07:24:43|02.11.2006 10:53:57|      |    |
                           |18.06.2007 07:24:44|18.06.2007 07:24:44|      |    |
                           |02.11.2006 07:25:08|18.09.2006 23:43:37|      |    |
                           |17.06.2007 21:33:35|07.05.2009 13:45:58|      |    |
                           |02.06.2008 20:30:11|02.06.2008 20:30:11|      |    |
                           |02.06.2008 20:30:11|02.06.2008 20:30:11|      |    |
                           |17.06.2007 21:26:06|07.05.2009 13:45:57|      |    |
[/pre]

Schlagwörter: DataTable Text Tabelle