Monday, September 10, 2018

C# - How add items to ListView with two columns

/* add two new columns */
            
listView1.Columns.Clear();
listView1.Columns.Add( "Name", 150, HorizontalAlignment.Left );
listView1.Columns.Add( "Detail", 100, HorizontalAlignment.Left );

listView1.View = View.Details;

/* enable row selection */
listView1.HideSelection = false;
listView1.FullRowSelect = true;

/* add three items */

ListViewItem lvi = new ListViewItem( "Item 1" );
listView1.Items.Add( lvi );
lvi.SubItems.Add( "Detail 1" );
lvi.Selected = true; 

lvi = new ListViewItem( "Item 2" );
listView1.Items.Add( lvi );
lvi.SubItems.Add( "Detail 2" );

lvi = new ListViewItem( "Item 3" );
listView1.Items.Add( lvi );
lvi.SubItems.Add( "Detail 3" );

listView1.Focus();
Output:

No comments:

Post a Comment