using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ArchiveSeek
{
public partial class Lytj : Form
{
public Lytj()
{
InitializeComponent();
}
DA jkj = new DA();//用户自定义类
private void Lytj_Load(object sender, EventArgs e)
{
ItemBind();
}
/// <summary>
/// ListView控件的数据绑定
/// </summary>
private void ItemBind()
{
this.listView1.Groups.Clear();//清空ListView分组
this.listView1.Items.Clear(); //清空ListView数据项
this.listView1.GridLines = true;
this.listView1.View = View.Details;
this.listView1.Columns.Clear();//清空ListView列
DataTable ItemDt =new DataTable();
ColumnHeader ch = null;
ItemDt = jkj.GetDataTable("select cdlb as 查询类别,count(userid) as 利用人次,count(rc) as 利用卷数 From viewly Group by cdlb order by cdlb");
for (int y = 0; y < ItemDt.Columns.Count; y++)
{//添加数据列。
ch = new ColumnHeader();
ch.Text = ItemDt.Columns[y].ColumnName.ToString();
ch.Name = ItemDt.Columns[y].ColumnName.ToString();
ch.Width = 150;
this.listView1.Columns.Add(ch);
}
DataTable zdt = jkj.GetDataSet("Select Distinct nd From viewly order by nd","nian").Tables["nian"];
ListViewGroup Lvg = null;
int mmm = 0;
for (int i = 0; i < zdt.Rows.Count; i++)
{//添加ListView分组
Lvg = new ListViewGroup(zdt.Rows[i][0].ToString().Trim()+"年");
mmm = int.Parse(zdt.Rows[i][0].ToString().Trim());
this.listView1.Groups.Add(Lvg);
ItemDt.Clear();//清空DataTable中的所有数据。
ItemDt = jkj.GetDataTable("select cdlb as 查询类别,count(userid) as 利用人次,count(rc) as 利用卷数 From viewly where nd=" + mmm + " Group by cdlb order by cdlb");
Method(ItemDt, Lvg);
}
}
/// <summary>
/// 为ListView绑定数据项
/// </summary>
/// <param name="dd">DataTable表</param>
/// <param name="zhang">ListViewGroup分组</param>
private void Method(DataTable dd,ListViewGroup zhang)
{
ListViewItem lvi = null;
for (int x = 0; x < dd.Rows.Count; x++)
{
lvi = new ListViewItem(dd.Rows[x][0].ToString());
for (int k = 1; k < dd.Columns.Count; k++)
{
lvi.ImageIndex = 0;
lvi.Group = zhang;
lvi.SubItems.Add(dd.Rows[x][k].ToString());
}
this.listView1.Items.Add(lvi);
}
}
}
}
第二种:
public partial class frmListViewGroup : Form
{
public frmListViewGroup()
{
InitializeComponent();
List<部门> l_下属部门 = new List<部门>();
l_下属部门.Add(new 部门 { Id = "0", 名称 = "哈尔滨公司0" });
l_下属部门.Add(new 部门 { Id = "0", 名称 = "哈尔滨公司1" });
l_下属部门.Add(new 部门 { Id = "0", 名称 = "哈尔滨公司2" });
List<部门> l_下属部门1 = new List<部门>();
l_下属部门1.Add(new 部门 { Id = "1", 名称 = "哈尔滨公司3" });
l_下属部门1.Add(new 部门 { Id = "1", 名称 = "哈尔滨公司4" });
l_下属部门1.Add(new 部门 { Id = "1", 名称 = "哈尔滨公司5" });
l_下属部门1.Add(new 部门 { Id = "1", 名称 = "哈尔滨公司6" });
this.l_部门.Add(new 部门 { Id = "0", 名称 = "哈尔滨公司", 下属部门列表 = l_下属部门 });
l_部门.Add(new 部门 { Id = "1", 名称 = "哈尔滨公司12", 下属部门列表 = l_下属部门1 });
}
List<部门> l_部门 = new List<部门>();
private void frmListViewGroup_Load(object sender, EventArgs e)
{
ListViewGroup Lvg = null;
for (int i = 0; i < this.l_部门.Count; i++)
{
Lvg = new ListViewGroup(this.l_部门[i].名称);
this.listView1.Groups.Add(Lvg);
Method(this.l_部门[i].下属部门列表.ToList(), Lvg);
}
}
/// <summary>
/// 为ListView绑定数据项
/// </summary>
/// <param name="_lstDept">List<部门></param>
/// <param name="_lvg">ListViewGroup分组</param>
private void Method(List<部门> _lstDept, ListViewGroup _lvg)
{
ListViewItem lvi = null;
for (int k = 1; k < _lstDept.Count; k++)
{
lvi = new ListViewItem(_lstDept[k].Id.ToString());
lvi.ImageIndex = 0;
lvi.Group = _lvg;
lvi.SubItems.Add(_lstDept[k].名称);
this.listView1.Items.Add(lvi);
}
}
}