1 /// <summary> 2 /// 計算ListView各類子項ListViewGroup的數量 3 /// </summary> 4 /// <returns></returns> 5 private DataTable getNumber(ListView listview) 6 { 7 ArrayList alist = new ArrayList(); 8 DataTable dt = new DataTable(); 9 dt.Columns.Add("name", typeof(string)); 10 dt.Columns.Add("number", typeof(int)); 11 //獲取各字段名稱 12 for (int i = 0; i < listview.Columns.Count; i++) 13 { 14 alist.Add(listview.Columns[i].Text); 15 } 16 17 foreach (ListViewGroup group in listview.Groups) 18 { 19 string b = group.Header; 20 int c = 0; 21 foreach (ListViewItem itemRow in group.Items) 22 { 23 24 for (int i = 0; i < itemRow.SubItems.Count; i++) 25 { 26 27 string s = itemRow.SubItems[i].Text; 28 if (s == b) 29 { 30 c++; 31 32 } 33 //getlist = ListViewGroupConvertDatat(lsvData, group); 34 35 } 36 37 } 38 getlist = ListViewGroupConvertDatat(lsvData, group); 39 //dt.Rows.Add(group.Header, c); 40 dt.Rows.Add(group.Header, getlist.Rows.Count); 41 group.Header = group.Header + " 共有(" + getlist.Rows.Count.ToString() + ")條記錄"; 42 //for (int i = 0; i < alist.Count; i++) 43 //{ 44 // string s = alist[i].ToString(); 45 // if (s == b) 46 // { 47 // getlist = ListViewGroupConvertDatat(lsvData, group); 48 // dt.Rows.Add(group.Header, getlist.Rows.Count); 49 // } 50 //} 51 52 } 53 return dt; 54 } 55 /// <summary> 56 /// ListView Convert DataTabe 57 /// </summary> 58 /// <param name="lstview"></param> 59 /// <returns></returns> 60 private DataTable ListViewConvertDataTabe(ListView lstview) 61 { 62 DataTable dt = new DataTable(); 63 //獲取各字段名稱 64 for (int i = 0; i < lstview.Columns.Count; i++) 65 { 66 //this.groupBox.Items.Add(lstview.Columns[i].Text); 67 dt.Columns.Add(lstview.Columns[i].Text, typeof(string)); 68 } 69 //for (int i = 0; i < lstview.Items.Count; i++) 70 //{ 71 // dt.Rows.Add(lstview.Items[i].SubItems[0].Text, lstview.Items[i].SubItems[1].Text, lstview.Items[i].SubItems[2].Text, lstview.Items[i].SubItems[3].Text, lstview.Items[i].SubItems[4].Text); 72 //} //Which Subitems you want to add in the listview 73 //var listView1 = new ListView(); 74 //DataTable table = new DataTable(); 75 76 foreach (ListViewItem item in lstview.Items) 77 { 78 DataRow dtRow = dt.NewRow(); 79 int c = 0; 80 foreach (ListViewItem.ListViewSubItem it in item.SubItems) 81 { 82 dtRow[c] = it.Text; 83 c++; 84 85 } 86 dt.Rows.Add(dtRow); 87 } 88 return dt; 89 } 90 /// <summary> 91 /// 計算ListView 中的子項集合ListViewGroup 92 /// </summary> 93 /// <param name="lstview"></param> 94 /// <param name="lstgroup"></param> 95 /// <returns></returns> 96 private DataTable ListViewGroupConvertDatat(ListView lstview, ListViewGroup lstgroup) 97 { 98 99 DataTable dt = new DataTable(); 100 //獲取各字段名稱 101 for (int i = 0; i < lstview.Columns.Count; i++) 102 { 103 //this.groupBox.Items.Add(lstview.Columns[i].Text); 104 dt.Columns.Add(lstview.Columns[i].Text, typeof(string)); 105 } 106 107 foreach (ListViewItem item in lstgroup.Items) 108 { 109 110 DataRow dtRow = dt.NewRow(); 111 int c = 0; 112 //dt.Columns.Add(item.ToString()); 113 //for (int i = 0; i < item.SubItems.Count; i++) 114 //{ 115 116 // dtRow[i] = item.SubItems[i].Text; 117 118 //} 119 //ListViewItem.ListViewSubItemCollection; 120 // ListViewItem.ListViewSubItem 121 ////ListViewGroupCollection 122 foreach (ListViewItem.ListViewSubItem it in item.SubItems) 123 { 124 125 dtRow[c] = it.Text; 126 c++; 127 } 128 dt.Rows.Add(dtRow); 129 } 130 131 132 return dt; 133 }
用法:在LISTVIEW 绑定数据后。调用:datatble getlist = getNumber(listview);