C # Winform about ListView Control binding DataTable

Source: Internet
Author: User

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
  1. Private void LoadData ()
  2. {
  3. String sql = @"SELECT * from";
  4. DataTable dt = dbutil.getdatatable (SQL);
  5. this.listView1.Columns.Clear (); //Good habits, first clear and then add to ensure consistency of data
  6. this.listView1.Columns.Add ("Roomid");
  7. this.listView1.Columns.Add ("Typeids");
  8. this.listView1.Columns.Add ("price");
  9. this.listView1.Items.Clear ();
  10. int length = dt.  Rows.Count;
  11. For (int i = 0; i < length; i++)
  12. {
  13. ListViewItem LVI = new ListViewItem (dt. rows[i]["Roomid"].  ToString ()); //listview's first item needs to be added separately as a master item
  14. string typeid = dt. rows[i]["Typeids"].  ToString ();
  15. switch (typeid)
  16. {
  17. case "1":
  18. Lvi.   ImageIndex = 0; //Set the type of picture that is bound for each item type
  19. Break ;
  20. case "2":
  21. Lvi. ImageIndex = 1;
  22. Break ;
  23. Case "3":
  24. Lvi. ImageIndex = 2;
  25. Break ;
  26. Default:
  27. Break ;
  28. }
  29. Lvi. SubItems.Add (dt. rows[i]["Typeids"].   ToString ()); //The item that is added later is subitems, which is the child
  30. Lvi. SubItems.Add (dt. rows[i]["Price"].  ToString ());
  31. This.listView1.Items.Add (LVI); //Last add
  32. }
  33. }

C # Winform about ListView Control binding DataTable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.