[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Easily add column headers and items to a ListView control in C#


Normally to add a row to a ListView control, you first add a new item and then add sub-items to the item. The following code shows an AddRow extension method for the ListView class that makes adding new rows easier.

// Add the items as a new row in the ListView control. public static void AddRow(this ListView lvw, string[] items) { // Make the main item. ListViewItem new_item = lvw.Items.Add(items[0]); // Make the sub-items. for (int i = 1; i < items.Length; i++) new_item.SubItems.Add(items[i]); }

This code takes a parameter array of strings to be added to the row. It uses the first value to make a new item. It then adds the other values as sub-items.

If lvwBooks is a ListView control, you can use this method as in the following:

lvwBooks.AddRow(new string[] { "C# 5.0 Programmer's Reference", "http://tinyurl.com/qzcefsp", "978-1-118-84728-2", "960", "2014" });

Because the list of items is a parameter array, you can also use the extension method as in the following:

lvwBooks.AddRow( "Beginning Database Design Solutions", "http://www.vb-helper.com/db_design.htm", "978-0-470-38549-4", "552", "2008");

To add column headers to a ListView control, you normally add new items to its Columns collection. The following MakeColumnHeaders extension method makes it easy to add headers all at once, specifying their titles and alignments.

// Make the ListView's column headers. // The ParamArray entries should alternate between // strings and HorizontalAlignment values. public static void MakeColumnHeaders(this ListView lvw, params object[] header_info) { if (header_info.Length % 2 != 0) throw new ArgumentException( "The method must have an even number " + "of header_info parameters"); // Remove any existing headers. lvw.Columns.Clear(); // Make the column headers. for (int i = 0; i < header_info.Length; i += 2) { lvw.Columns.Add( (string)header_info[i], -1, (HorizontalAlignment)header_info[i + 1]); } }

The method first verifies that the header_info parameter array contains an even number of items. It then clears the ListView control's Columns collection. Finally, it adds the header values specifying a column width of -1 (size to fit the data) and the horizontal alignment passed in through the parameter array.

The main program uses the following code to call this extension method.

// Make the ListView column headers. lvwBooks.MakeColumnHeaders( "Title", HorizontalAlignment.Left, "URL", HorizontalAlignment.Left, "ISBN", HorizontalAlignment.Left, "Pages", HorizontalAlignment.Right, "Year", HorizontalAlignment.Right );

The program does two other interesting things with its ListView control. First, it uses the following extension method to set all of the control's column sizes at once.

// Set all columns' sizes. public static void SizeColumns(this ListView lvw, int size) { for (int i = 0; i < lvw.Columns.Count; i++) lvw.Columns[i].Width = -2; }

The main program uses the following statement to call this method.


Finally, the main program uses the following code to size the form to fit the ListView control.

// Make the form big enough to show the ListView. Rectangle item_rect = lvwBooks.GetItemRect(lvwBooks.Items.Count - 1); this.ClientSize = new Size( item_rect.Left + item_rect.Width + 25, item_rect.Top + item_rect.Height + 25);

The code uses the ListView control's GetItemRect method to get the bounding rectangle for the last item in the control. It then uses that rectangle's dimensions to make the form big enough to display the whole ListView at once.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.