Make a program generate HTML table code for ListView data and display it in a WebBrowser control in C#

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 that displays the group's name spanning all of the columns. It then loops through the group's Items property displaying each of the items.

The code uses two functions to make pieces of this process easier: ListViewColumnHeaderHtml and ListViewItemHtml.

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

The ListViewItemHtml function loops through an item's SubItems collection to display the item's values. Conveniently the SubItems collection includes the item itself in its 0th entry so the program doesn't need to consider the main item separately.

When it displays any item, the code checks the column's alignment so the item is properly aligned in the HTML table.

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 details.

   

 

What did you think of this article?




Trackbacks
  • 2/20/2014 12:14 AM BLOG.CSHARPHELPER.COM wrote:
    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. ...
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.