C#中ListView的简单使用方法

ListView是用于显示数据的,先在窗体中拉一个lisview控件,还有一些新增、修改、删除、查询按钮和文本框,控件名称为listview,按钮为btnInsert,btnUpate,btnDeleteOne,btnDelete,btnSelect,文本框的名称为txtName,txtSex,txtPhone,txtAddress,设计如下图所示:

C#中ListView的简单使用方法

写完这些后,先设置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 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;
            //创建行对象
            ListViewItem li = new ListViewItem(name);
            //添加同一行的数据
            li.SubItems.Add(sex);
            li.SubItems.Add(phone);
            li.SubItems.Add(address);
            //将行对象绑定在listview对象中
            lv.Items.Add(li);

    MessageBox.Show("新增数据成功!");
            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;  //显示网格线
            lv.FullRowSelect = true;  //显示全行
            lv.MultiSelect = false;  //设置只能单选
            lv.View = View.Details;  //设置显示模式为详细
            lv.HoverSelection = true;  //当鼠标停留数秒后自动选择
            //把列名添加到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("修改数据成功!");
            }
            return lv;
        }
       
        /// <summary>
        /// 当listview选中状态改变时调用的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {

    //当有选择行的数据时
            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) {
            //列表有数据
            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("未输入列表数据");
            }
        }
    }
}

最后运行效果是这样的,这是新增数据时的效果

C#中ListView的简单使用方法

这是修改数据后的效果:

C#中ListView的简单使用方法

这是查询是否存在这个名字的效果:

C#中ListView的简单使用方法

删除选中行和删除全部就不显示出来了。

上一篇:Amoeba for MySQL读写分离配置


下一篇:Div+Css布局教程(-)CSS必备知识