|
|
|
|
|
|
|
Author Rank :
|
|
|
Page Views :
|
232555
|
|
Downloads :
|
1878
|
|
Rating :
|
Rate it
|
|
Level :
|
Beginner
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
|
|
There are many questions about adding multiple columns to a ListView control. Some times its hard for beginners to find even small clues like ListView multi column won't work until you set its View property as Details.
Any way, this small article explains how to create a multi column list view control and add data to it.
Creating Project
First create a Windows Application project in Visual Studio .NET and drop a ListView control from toolbox to the form. After that first thing you need to do is to set View property of list view control to Details. See figure 1.

Figure 1. Setting ListView Control's View Property to Details.
The View property can have any of following members of View enum.
| Member |
Description |
| Details |
Each item appears on a separate line with further information about each item arranged in columns. The left most column contains a small icon and label, and subsequent columns contain sub items as specified by the application. A column displays a header which can display a caption for the column. The user can resize each column at runtime. |
| LargeIcon |
Each item appears as a full-sized icon with a label below it. |
| List |
Each item appears as a small icon with a label to its right. Items are arranged in columns with no column headers. |
| SmallIcon |
Each item appears as a small icon with a label to its right. | You can also set this property programmatically by using the following code:
ListView1.View = System.Windows.Forms.View.Details
Adding Multiple Columns
Now I add five columns to the control. You can add column headers to the control using its Columns.Add method. The Columns.Add method takes column header name, its width, and alignment.
In our sample, I write code at Form_Load method to do so. See Listing 1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Adding ListView Columns ListView1.Columns.Add("Emp Name", 100, HorizontalAlignment.Left) ListView1.Columns.Add("Emp Address", 150, HorizontalAlignment.Left)ListView1.Columns.Add("Title", 60, HorizontalAlignment.Left) ListView1.Columns.Add("Salary", 50, HorizontalAlignment.Left)ListView1.Columns.Add("Department", 60, HorizontalAlignment.Left) End Sub
Listing 1. Adding Column Headers of a ListView Control
After adding this code, the output of program looks like Figure 2.

Figure 2. Multiple Column Headers
Adding Records
ListView.Item.Add method adds records to a list view control. The Add method takes ListViewItem type value. For multi column list view control, we create a ListViewItem with an array of 5 string items. The following code adds a record to the list view control.
Dim str(5) As String Dim itm As ListViewItem str(0) = "Rob Machy" str(1) = "100 North Ave" str(2) = "Business Manager" str(3) = "89,000" str(4) = "Development" itm = New ListViewItem(str) ListView1.Items.Add(itm)
After setting more properties of the list view control and adding more records, our list program looks like Figure 3.

Figure 3. Multi Column ListView Control
|
|
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post
Here.
|
|
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle. If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
|
|
|
|
|
|
|
|
|
|
|
|
|