The ListView is a more complex control
1. Define drag it in, the system will automatically add a this.listview1= new System.Windows.Forms.ListView () in the Designer.cs; 2. Initialize, determine the pattern, determine the column InitializeComponent () the system first automatically completes its initialization operation and the user-defined initialization operation I suggest to write in the form constructor followed by InitializeComponent (). The reason is that once the ListView changes, the system automatically adjusts the InitializeComponent () content, which may automatically delete the user-written portion.
THIS.LISTVIEW1.COLUMNS.ADD ("Student number"), THIS.LISTVIEW1.COLUMNS.ADD ("Student name"), THIS.LISTVIEW1.COLUMNS.ADD ("student number"); These are the main ones, which determine altogether several columns This.listView1.View = System.Windows.Forms.View.Details; If you do not write this sentence, the added column does not show. Must be written. Let's look at the ListView usage in details mode to assign values to the ListView view property. Many tutorials are written directly this.listView1.View =view.details; System.Windows.Forms.View is a enum public enum View;LargeIconEach item is displayed as a maximized icon with a label underneath it.DetailsEach item appears on a different line with further information about the items that are arranged in the column. The leftmost column contains a small icon and a label, followed by a column that contains the child items specified by the application. column displays a header that can display the column's title. The user can adjust the size of the columns at run time.SmallIconEach item is displayed as a small icon with a label on its right.ListEach item is displayed as a small icon with a label on its right. The items are arranged in a column with no column headers.TileEach item is displayed as a full-sized icon with item labels and subkey information on its right. The displayed subkey information is specified by the application. This view is only supported on the following platforms: WindowsXP and Windows Server 2003 series. On the previous operating system, this value is ignored, and the ListView control is displayed in the LargeIcon view. Listview1.fullrowselect = True;this.listview1.gridlines = True;listview1.labeledit = true;//allow the user to rearrange C Olumns.listView1.AllowColumnReorder = true;//Display Check boxes.listView1.CheckBoxes = true;listview1.sorting = Sortorder.ascending;//create three items and three sets of subitems for Eachitem. In addition, there are many custom attributes can be equipped with, what selected, the line, try it yourself the default column width is not large, the title header display Total Listview1.columns[0]. Width = 100;
LISTVIEW1.COLUMNS[1]. The Width = 100; columns array itself can refer to each column 3. Add row add row operations are mainly divided into two, one is the direct string addition, one is the use of ListViewItem object add A. Direct Add method this.listview1.items.add (" A1 "); This means adding a new row to the control table and setting the first column of the new row to A1, at which point the collection of elements in the new row is this.listview1.item[0]this.listview1.items[0]. SubItems.Add ("A2"); Add the second column of data A2 to the first line that you just added, which is this.listview1.item[0]. If items are added at the level of the row, Item[i]. SubItems does not really correspond to cell operations. This.listview1.items[0]. SubItems.Add ("A3"); at this time the first line to add work ends, Uncle White Sue You, the first line of content for A1---A2---a3 this.listview1.items.add ("B1"); at this time add , which is equivalent to adding the second row of data, plus the cell contents of the first column in the second row. THIS.LISTVIEW1.ITEMS[1]. SubItems.Add ("B2"); items means rows, so the second line is items[1], and the row label starts at 0. THIS.LISTVIEW1.ITEMS[1]. SubItems.Add ("B3"); end of the second line, content B1---B2---b3 two. ListViewItem indirect addition method. method is to first create a row object and then add this row object to This.listview1.items listviewitem Li initialization and there are two ways, similar one is ListViewItem li=new ListViewItem ("A1"); another is listviewitem li=new ListViewItem (), Li. text= "A1"; role is the same, white uncle does not cheat you. Initializes a "Row object" while initializing the first column cell of the Row object.For "A1" li. SubItems.Add ("abc"); Li. SubItems.Add ("CDE"); then add the contents of the second and third columns. this.listview1.items.add (LI); This sentence before a few words also line, after the line can be added to the ListView before adding children, you can add all the children and then added Listview li can be reused, and constantly initialized into a new line to add. about item selection and data collection first of all, the selection of the ListView item is divided into two categories, one is the left-button selection, The applicable method is SelectedItems and selectedindices the other is the selection by check box selected CheckedItems I have not used, for the time being not discussed, but in fact similar to the first
Left button to select
1. First of all to determine whether there is a selection of blue, if you do not judge the use of selecteditem[i] will be because there is no appropriate result error. if (this.listview1.selecteditems.count> 0)
2. SelectedItems is a collection of selected rows, that is, multi-row selection, which is a collection of multiple rows. This.textBox2.Text =this.listview1.selecteditems[0]. The index [0] here does not refer to the first row of the original list, but rather to the entire selected row as a set, the first selected row in the selected row. The cell contents of the first column of the row are returned. This.textBox2.Text =this.listview1.selecteditems[0]. Subitems[0]. Text, using subitems[0] both get the same result, and are the first column of the first selected row. This.textBox2.Text =this.listview1.selecteditems[0]. SUBITEMS[1]. Text above is the second column of the first selected row, and so on.
3. SelectedIndices is the index value that returns the selected row Intthis.listview1.selectedindices[i] is also the selection of rows as a whole, with I to locate.
If only the selection of a single row to get a judge Listview1.focuseditem. Text estimation is more efficient, try it yourself, uncle don't write
C # ListView Usage