The ListView is used in WinForm for table construction, which can visually display the table's information in the form of a SQL table
This is his position in the common controls:
Several important properties of the ListView: Columms (Collection), Groups (collection), Items (collection), view (view), and subitems (collection) in items
There are 5 styles of view, the most common option is details (detail)
Divides the collection name of each row column under the details style in the ListView.
Columns collection: in which column names need to be edited, first row (column name)
Items collection, the second row of the first column starts, and this column is a separate collection
SubItems collection: Starting with the second row in the second column, each behavior is a collection.
Note: SubItems in the Properties section of the Items Collection Editor
Learn about the structure of the ListView and see how to add data to it using code.
1. Create a WinForm, draw a ListView in the table, and modify the properties of the ListView:
View is modified to details.
Add members to the properties--behavior--columns collection and change text to number, name, gender, birthday, class, respectively
Create a new class: Xuesheng
Public class Xuesheng { //define variables and encapsulate.
Private string_sno; Public stringSno {Get{return_sno;} Set{_sno =value;} } Private string_sname; Public stringSname {Get{return_sname;} Set{_sname =value;} } Private string_ssex; Public stringSsex {Get{return_ssex;} Set{_ssex =value;} } Private string_sbirthday; Public stringSbirthday {Get{return_sbirthday;} Set{_sbirthday =value;} } Private string_class; Public stringClass {Get{return_class;} Set{_class =value;} } PublicDataTable Goujianbiao ()//Create a temporary data table in C #{DataTable dt=NewDataTable ();//to initialize the data table, you need to reference the using System.Data; //add a column to a tableDataColumn sno =NewDataColumn ("Sno");//column of table (column name)dt. Columns.Add (SNO); DataColumn sname=NewDataColumn ("sname"); Dt. Columns.Add (sname); DataColumn Ssex=NewDataColumn ("Ssex"); Dt. Columns.Add (Ssex); DataColumn Sbirthday=NewDataColumn ("Sbirthday"); Dt. Columns.Add (Sbirthday); DataColumn Sclass=NewDataColumn ("Sclass"); Dt. Columns.Add (Sclass); //Create rows for a tableDataRow row1=dt. NewRow (); //fill in rows with datarow1["Sno"]="101"; row1["sname"]="Zhang San"; row1["Ssex"]="male"; row1["Sbirthday"]="1987-05-15"; row1["Sclass"]="Class 2012"; Dt. Rows.Add (ROW1);//fills the row Row1 into the set of rows of the table dt returnDT; //Return Data Table object }
Enter the following code in the From_load function:
Private voidForm3_load (Objectsender, EventArgs e) {Xuesheng xx=NewXuesheng ();//If you are no longer in the same namespace, do not forget to refer to namespacesDataTable dt = Xx.goujianbiao ();//call the method to build the DataTable table data for(inti =0; i < dt. Rows.Count; i++)//dt. Rows.Count Get data table number of rows in dt{DataRow Dr= dt. Rows[i];//Building a row of data in a data table, a collection of rows, where there is only one row of data,//find the element of this line based on the column name and add it to the Items collection, the second row of the first column, where the items collection has only one elementLISTVIEW1.ITEMS.ADD (dr["Sno"]. ToString ()); //Based on column nameAdd the elements of the subitems collection separately, start with the second row of the second column, and arrange the elements in the collection by rowListview1.items[i]. SubItems.Add (dr["sname"]. ToString ()); Listview1.items[i]. SubItems.Add (dr["Ssex"]. ToString ()); Listview1.items[i]. SubItems.Add (dr["Sbirthday"]. ToString ()); Listview1.items[i]. SubItems.Add (dr["Sclass"]. ToString ()); } }
Last Run Result:
The ListView parsing in 20141226-c# WinForm