程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#中ListView控件完成窗體代碼

C#中ListView控件完成窗體代碼

編輯:C#入門知識

C#中ListView控件完成窗體代碼。本站提示廣大學習愛好者:(C#中ListView控件完成窗體代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中ListView控件完成窗體代碼正文


空話不多說了,直接給年夜家貼症結代碼了。

詳細代碼以下所示:

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控件完成窗體代碼,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved