Today we use the ListView control, which summarizes the way the ListView control binds to the DataTable
It is important to note that the binding process implements the property of the bound control's column and then the item's binding.
Unlike DataGridView, you cannot use datasourse bindings directly,
Furthermore, when binding the first items, be sure to instantiate an item for a separate binding, because the first item is the difference from the next without the item to the subitem, and the corresponding comment is given in the code below.
[CSharp]View PlainCopy
- Private void LoadData ()
- {
- String sql = @"SELECT * from";
- DataTable dt = dbutil.getdatatable (SQL);
- this.listView1.Columns.Clear (); //Good habits, first clear and then add to ensure consistency of data
- this.listView1.Columns.Add ("Roomid");
- this.listView1.Columns.Add ("Typeids");
- this.listView1.Columns.Add ("price");
- this.listView1.Items.Clear ();
- int length = dt. Rows.Count;
- For (int i = 0; i < length; i++)
- {
- ListViewItem LVI = new ListViewItem (dt. rows[i]["Roomid"]. ToString ()); //listview's first item needs to be added separately as a master item
- string typeid = dt. rows[i]["Typeids"]. ToString ();
- switch (typeid)
- {
- case "1":
- Lvi. ImageIndex = 0; //Set the type of picture that is bound for each item type
- Break ;
- case "2":
- Lvi. ImageIndex = 1;
- Break ;
- Case "3":
- Lvi. ImageIndex = 2;
- Break ;
- Default:
- Break ;
- }
- Lvi. SubItems.Add (dt. rows[i]["Typeids"]. ToString ()); //The item that is added later is subitems, which is the child
- Lvi. SubItems.Add (dt. rows[i]["Price"]. ToString ());
- This.listView1.Items.Add (LVI); //Last add
- }
- }
C # Winform about ListView Control binding DataTable