Use ListView data to generate HTML TABLE code in C#

generate HTML TABLE

The code for this program is long so I won’t include it all here, but the ideas are straightforward enough. The program builds a string holding the HTML text needed to represent the table.

It starts by determining whether the ListView has groups. It does that by looping through the ListView‘s items checking each one to see if its Group property is null. (For information about using ListView groups, see Use groups in a ListView control in C#.)

If the control includes non-grouped items, then the code builds HTML code to display the ListView’s column headers, and then loops through the items again displaying those that are not in groups. This mimics the way the ListView displays its non-grouped items.

Next, the program loops through the groups in the ListView‘s Groups collection. For each group, the code makes a header row spanning all of the table’s columns that displays the group’s name. It then loops through the group’s Items property and displays each of the items. The code uses two methods to make pieces of this process easier: ListViewColumnHeaderHtml and ListViewItemHtml.

The ListViewColumnHeaderHtml method returns HTML code to display column headers. It loops through the ListView control’s Columns collection to generate the column headers.

The ListViewItemHtml method loops through an item’s subItems collection to display the item’s values. The subItems collection conveniently includes the item itself in its 0th entry, so the program doesn’t need to consider the main item separately. It can just treat it as another sub-item.

When it displays any item, the code checks the column’s alignment and gives the entry in the HTML table the same alignment.

Finally, when the HTML table is complete, the program uses the following code to display it in a WebBrowser control.

wbrTable.DocumentText = ListViewToHtmlTable(lvwResults, 1, 1, 2);

See the code for additional details.

Download Example   Follow me on Twitter   RSS feed

This entry was posted in controls, html, internet, user interface, web and tagged , , , , , , , , , , , , , . Bookmark the permalink.

4 Responses to Use ListView data to generate HTML TABLE code in C#

  1. Use custom numeric formats in C#

    You can use custom numeric formatting characters to display date and time values in particular formats. These formats work with a variable’s ToString method as well as with String.Format. For example, the code (1.234).ToString(“”0.00”) returns the value “1.23.” This example builds a ListView that describes the various custom numeric format characters. It then builds another ListView that shows examples. Finally the program uses code from the example Make a program generate HTML table code for ListView data and display it in a WebBrowser control in C# to convert the results into the following HTML tables. …

  2. Pingback: Use the DateTime class's standard formatting methods in C# -

  3. Pingback: Use custom numeric formats in C# -

  4. Pingback: Use standard date and time formats in C# -

Comments are closed.