ListView是用于顯示數(shù)據(jù)的,先在窗體中拉一個lisview控件,還有一些新增、修改、刪除、查詢按鈕和文本框,控件名稱為listview,按鈕為btnInsert,btnUpate,btnDeleteOne,btnDelete,btnSelect,文本框的名稱為txtName,txtSex,txtPhone,txtAddress,設計如下圖所示:
寫完這些后,先設置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 TestGame
{
public partial class ListViewForm : Form
{
public ListViewForm()
{
InitializeComponent();
}
/// <summary>
/// 窗體加載時調用初始化的Listview方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListViewForm_Load(object sender, EventArgs e)
{
InitListView(this.listView);
}
/// <summary>
/// 當點擊新增時調用的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnInsert_Click(object sender, EventArgs e)
{
InsertListView(this.listView);
}
/// <summary>
/// 當點擊修改時調用的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnUpdate_Click(object sender, EventArgs e)
{
UpdateListView(this.listView);
}
/// <summary>
/// 新增方法
/// </summary>
/// <param name="lv"></param>
/// <returns></returns>
public ListView InsertListView(ListView lv) {
//獲取文本框中的值
string name = this.txtName.Text;
string sex = this.txtSex.Text;
string phone = this.txtPhone.Text;
string address = this.txtAddress.Text;
//創(chuàng)建行對象
ListViewItem li = new ListViewItem(name);
//添加同一行的數(shù)據(jù)
li.SubItems.Add(sex);
li.SubItems.Add(phone);
li.SubItems.Add(address);
//將行對象綁定在listview對象中
lv.Items.Add(li);
MessageBox.Show("新增數(shù)據(jù)成功!");
return lv;
}
/// <summary>
/// 初始化ListView的方法
/// </summary>
/// <param name="lv"></param>
public void InitListView(ListView lv) {
//添加列名
ColumnHeader c1 = new ColumnHeader();
c1.Width = 100;
c1.Text = "姓名";
ColumnHeader c2 = new ColumnHeader();
c2.Width = 100;
c2.Text = "性別";
ColumnHeader c3 = new ColumnHeader();
c3.Width = 100;
c3.Text = "電話";
//設置屬性
lv.GridLines = true; //顯示網(wǎng)格線
lv.FullRowSelect = true; //顯示全行
lv.MultiSelect = false; //設置只能單選
lv.View = View.Details; //設置顯示模式為詳細
lv.HoverSelection = true; //當鼠標停留數(shù)秒后自動選擇
//把列名添加到listview中
lv.Columns.Add(c1);
lv.Columns.Add(c2);
lv.Columns.Add(c3);
lv.Columns.Add("籍貫", 100); //相當于上面的添加列名的步驟
}
/// <summary>
/// 修改的方法
/// </summary>
/// <param name="lv"></param>
/// <returns></returns>
public ListView UpdateListView(ListView lv){
if (lv.SelectedItems.Count > 0) {
//把修改后的文本框內容添加到listview中
lv.SelectedItems[0].SubItems[0].Text = this.txtName.Text;
lv.SelectedItems[0].SubItems[1].Text = this.txtSex.Text;
lv.SelectedItems[0].SubItems[2].Text = this.txtPhone.Text;
lv.SelectedItems[0].SubItems[3].Text = this.txtAddress.Text;
MessageBox.Show("修改數(shù)據(jù)成功!");
}
return lv;
}
/// <summary>
/// 當listview選中狀態(tài)改變時調用的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listView_SelectedIndexChanged(object sender, EventArgs e)
{
//當有選擇行的數(shù)據(jù)時
if (this.listView.SelectedItems.Count > 0) {
//把選擇的信息顯示在相應的文本框中
this.txtName.Text = this.listView.SelectedItems[0].SubItems[0].Text;
this.txtSex.Text = this.listView.SelectedItems[0].SubItems[1].Text;
this.txtPhone.Text = this.listView.SelectedItems[0].SubItems[2].Text;
this.txtAddress.Text = this.listView.SelectedItems[0].SubItems[3].Text;
}
}
/// <summary>
/// 移除選中行的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDeleteOne_Click(object sender, EventArgs e)
{
if (this.listView.SelectedItems.Count > 0) {
//移除整一行
this.listView.SelectedItems[0].Remove();
}
}
/// <summary>
/// 移除所有行的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDeleteAll_Click(object sender, EventArgs e)
{
//清空整個listview中的行
this.listView.Items.Clear();
}
private void btnSelect_Click(object sender, EventArgs e)
{
SelectListView(this.listView);
}
public void SelectListView(ListView lv) {
//列表有數(shù)據(jù)
if (lv.Items.Count > 0)
{
foreach (ListViewItem li in lv.Items)
{
if (li.SubItems[0].Text == this.txtExitName.Text)
{
MessageBox.Show("存在該名稱");
return;
}
}
MessageBox.Show("沒有找到該姓名");
}
else {
MessageBox.Show("未輸入列表數(shù)據(jù)");
}
}
}
}
最后運行效果是這樣的,這是新增數(shù)據(jù)時的效果
這是修改數(shù)據(jù)后的效果:
這是查詢是否存在這個名字的效果:
刪除選中行和刪除全部就不顯示出來了。
該文章在 2021/3/8 11:14:59 編輯過