欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

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

来源:本站原创|时间:2020-01-10|栏目:C#教程|点击:

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

具体代码如下所示:

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控件实现窗体代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

上一篇:C#计算汽车行驶方向的方法分析

栏    目:C#教程

下一篇:System.Data.OleDb.OleDbException: 未指定的错误的完美解决方法

本文标题:C#中ListView控件实现窗体代码

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/6261.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有