listview 控件

Posted yp秋水伊人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了listview 控件相关的知识,希望对你有一定的参考价值。

 

private void Form1_Load(object sender, EventArgs e)
{

//设置该listview关联的imagelist
listView1.LargeImageList = imageList2;
listView1.SmallImageList = imageList1;

//显示详细信息
listView1.View = View.Details;

//添加组
ListViewGroup lg1 = new ListViewGroup();
lg1.Header = "一组";
lg1.Name = "one";
listView1.Groups.Add(lg1);
ListViewGroup lg2 = new ListViewGroup();
lg2.Header = "二组";
lg2.Name = "two";
listView1.Groups.Add(lg2);

//添加列
listView1.Columns.Add("代号");
listView1.Columns.Add("姓名");
listView1.Columns.Add("生日");

//选中整行
listView1.FullRowSelect = true;

//显示checkbox
listView1.CheckBoxes = true;

//添加项,listview每一条数据称为一项
InfoDA da = new InfoDA();
List<Info> list = da.Select();

foreach (Info data in list)
{
//造主项
ListViewItem zhuxiang = new ListViewItem();
zhuxiang.Text = data.Code;
zhuxiang.ImageIndex = 0;
zhuxiang.Group = listView1.Groups[0];

//造子项
ListViewItem.ListViewSubItem zi1 = new ListViewItem.ListViewSubItem();
zi1.Text = data.Name;
zhuxiang.SubItems.Add(zi1);

ListViewItem.ListViewSubItem zi2 = new ListViewItem.ListViewSubItem();
zi2.Text = data.Birthday.ToString("yyyy-MM-dd"); ;
zhuxiang.SubItems.Add(zi2);


//添加项
listView1.Items.Add(zhuxiang);

}


foreach (Info data in list)
{
//造主项
ListViewItem zhuxiang = new ListViewItem();
zhuxiang.Text = data.Code;
zhuxiang.ImageIndex = 1;
zhuxiang.Group = listView1.Groups[1];

//造子项
ListViewItem.ListViewSubItem zi1 = new ListViewItem.ListViewSubItem();
zi1.Text = data.Name;
zhuxiang.SubItems.Add(zi1);

ListViewItem.ListViewSubItem zi2 = new ListViewItem.ListViewSubItem();
zi2.Text = data.Birthday.ToString("yyyy-MM-dd"); ;
zhuxiang.SubItems.Add(zi2);


//添加项
listView1.Items.Add(zhuxiang);

}


}

private void button1_Click(object sender, EventArgs e)
{
listView1.View = View.Details;
}

private void button2_Click(object sender, EventArgs e)
{
listView1.View = View.LargeIcon;
}

private void button3_Click(object sender, EventArgs e)
{
listView1.View = View.SmallIcon;
}

private void button4_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
MessageBox.Show(listView1.SelectedItems[0].Text);
}
}

private void button5_Click(object sender, EventArgs e)
{
//listView1.CheckBoxes = true;
}

private void button6_Click(object sender, EventArgs e)
{
string s = "";

foreach (ListViewItem item in listView1.CheckedItems)
{
s += item.Text+"--";
}

MessageBox.Show(s);
}

以上是关于listview 控件的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET中找不到listview控件....急..怎么在ASP.NET中添加LISTVIEW控件...

c#将list集合直接绑定到listview控件

C# winform编程 开发环境VS2010 listview控件问题

VB_listview控件如何设置各列的默认宽度?

VB_listview控件如何设置各列的默认宽度?

c# listview 怎么添加图片