private void LoadData()
{
string sql = @"select 编号,姓名,性别,住址 from 学生表";
DataTable dt = DBUtil.GetData(sql);
if (dt.Rows.Count==0)
{
return;
}
//listview的绑定数据方式--先声明列
this.listView1.Columns.Clear();
this.listView1.Columns.Add("编号");
this.listView1.Columns.Add("姓名");
this.listView1.Columns.Add("性别");
this.listView1.Columns.Add("住址");
int count = dt.Rows.Count;
//再为行赋值,for循环赋值
for (int i = 0; i < count; i++)
{
//要先声明ListViewItem实例
ListViewItem item = new ListViewItem(dt.Rows[i]["编号"].ToString());
item.SubItems.Add(dt.Rows[i]["姓名"].ToString());
item.SubItems.Add(dt.Rows[i]["性别"].ToString());
item.SubItems.Add(dt.Rows[i]["住址"].ToString());
this.listView1.View = View.Details;
this.listView1.Items.Add(item);
}
//建立分组
this.listView1.Groups.Add(new ListViewGroup("男"));
this.listView1.Groups.Add(new ListViewGroup("女"));
for (int i = 0; i < this.listView1.Items.Count; i++)
{
//获取子列的值,根据性别这一列分组
if (this.listView1.Items[i].SubItems[2].Text.ToString() == "男")
{
this.listView1.Groups[0].Items.Add(this.listView1.Items[i]);
}
else
{
this.listView1.Groups[1].Items.Add(this.listView1.Items[i]);
}
}
}
C#ListView控件的相关用法
本文转载:CSDN博客