/// <summary> /// 計算ListView各類子項ListViewGroup的數量 /// </summary> /// <returns></returns> private DataTable getNumber(ListView listview) { ArrayList alist = new ArrayList(); DataTable dt = new DataTable(); dt.Columns.Add("name", typeof(string)); dt.Columns.Add("number", typeof(int)); //獲取各字段名稱 for (int i = 0; i < lsvData.Columns.Count; i++) { alist.Add(lsvData.Columns[i].Text); } foreach (ListViewGroup group in listview.Groups) { string b = group.Header; int c = 0; foreach (ListViewItem itemRow in group.Items) { for (int i = 0; i < itemRow.SubItems.Count; i++) { string s = itemRow.SubItems[i].Text; if (s == b) { c++; } //getlist = ListViewGroupConvertDatat(lsvData, group); } } getlist = ListViewGroupConvertDatat(lsvData, group); //dt.Rows.Add(group.Header, c); dt.Rows.Add(group.Header, getlist.Rows.Count); group.Header = group.Header + " 共有(" + getlist.Rows.Count.ToString() + ")條記錄"; //for (int i = 0; i < alist.Count; i++) //{ // string s = alist[i].ToString(); // if (s == b) // { // getlist = ListViewGroupConvertDatat(lsvData, group); // dt.Rows.Add(group.Header, getlist.Rows.Count); // } //} } return dt; } /// <summary> /// ListView Convert DataTabe /// </summary> /// <param name="lstview"></param> /// <returns></returns> private DataTable ListViewConvertDataTabe(ListView lstview) { DataTable dt = new DataTable(); //獲取各字段名稱 for (int i = 0; i < lstview.Columns.Count; i++) { //this.groupBox.Items.Add(lstview.Columns[i].Text); dt.Columns.Add(lstview.Columns[i].Text, typeof(string)); } //for (int i = 0; i < lstview.Items.Count; i++) //{ // 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); //} //Which Subitems you want to add in the listview //var listView1 = new ListView(); //DataTable table = new DataTable(); foreach (ListViewItem item in lstview.Items) { DataRow dtRow = dt.NewRow(); int c = 0; foreach (ListViewItem.ListViewSubItem it in item.SubItems) { dtRow[c] = it.Text; c++; } dt.Rows.Add(dtRow); } return dt; } /// <summary> /// 計算ListView 中的子項集合ListViewGroup /// </summary> /// <param name="lstview"></param> /// <param name="lstgroup"></param> /// <returns></returns> private DataTable ListViewGroupConvertDatat(ListView lstview, ListViewGroup lstgroup) { DataTable dt = new DataTable(); //獲取各字段名稱 for (int i = 0; i < lstview.Columns.Count; i++) { //this.groupBox.Items.Add(lstview.Columns[i].Text); dt.Columns.Add(lstview.Columns[i].Text, typeof(string)); } foreach (ListViewItem item in lstgroup.Items) { DataRow dtRow = dt.NewRow(); int c = 0; //dt.Columns.Add(item.ToString()); //for (int i = 0; i < item.SubItems.Count; i++) //{ // dtRow[i] = item.SubItems[i].Text; //} //ListViewItem.ListViewSubItemCollection; // ListViewItem.ListViewSubItem ////ListViewGroupCollection foreach (ListViewItem.ListViewSubItem it in item.SubItems) { dtRow[c] = it.Text; c++; } dt.Rows.Add(dtRow); } return dt; }
时间: 2024-10-10 12:58:33