欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中ListView控件实现窗体代码

程序员文章站 2022-11-01 14:31:57
废话不多说了,直接给大家贴关键代码了。 具体代码如下所示: using system; using system.collections.generic;...

废话不多说了,直接给大家贴关键代码了。

具体代码如下所示:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.forms;
namespace windowsformsapplication1
{
public partial class form1 : form
{
public form1()
{
initializecomponent();
}
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);
}
}
}

以上所述是小编给大家介绍的c#中listview控件实现窗体代码,希望对大家有所帮助