C# ListView控件制作表格:添加行,删除行,右键删除行

来源:互联网 发布:unity3d打砖块 源代码 编辑:程序博客网 时间:2024/04/29 17:57

界面:
这里写图片描述
例程代码:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace ListViewTest3{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();            comboBox1.Text = "男";   //性别下拉框初始化            //表格生成            lv.GridLines  = true;//表格是否显示网格线            lv.FullRowSelect = true;//是否选中整行            lv.Font = new Font("宋体", 12, lv.Font.Style);//设置字体            lv.View = View.Details;//设置显示方式            lv.Scrollable = true;//是否自动显示滚动条            lv.MultiSelect = true;//是否可以选择多行            //添加标题栏(表头)            lv.Columns.Add("ID",50);    //第一列只会左对齐,设置对齐方式没用            lv.Columns.Add("姓名", 80,HorizontalAlignment.Center);            lv.Columns.Add("性别", 80,HorizontalAlignment.Center);            lv.Columns.Add("籍贯", 80,HorizontalAlignment.Center);        }        /******************  添加行  *********************/        //添加一行内容,在点击“确定”按钮时调用        public void addlv(string ys, string ys2, string name, string sex, string add)        {            ListViewItem i_item = new ListViewItem();            //设置文字颜色            if (ys == "green")                i_item.ForeColor = Color.Green;            else if (ys == "red")                i_item.ForeColor = Color.Red;            //设置行背景色            if (ys2 == "Pink")                i_item.BackColor = Color.Pink;            else if (ys2 == "LightBlue")                i_item.BackColor = Color.LightBlue;            //添加行的第一列内容            i_item.SubItems[0].Text = (lv.Items.Count + 1).ToString();  //ID            //添加行的其他列内容            i_item.SubItems.Add(name);      //姓名            i_item.SubItems.Add(sex);       //性别            i_item.SubItems.Add(add);       //籍贯            lv.Items.Add(i_item);        }        /******************  添加行  *********************/        //“确定”按钮,添加一行        private void button_ok_Click(object sender, EventArgs e)        {            string color1, color2;            if (radioButton1.Checked == true)                color1 = "green";            else                color1 = "red";            if (radioButton3.Checked == true)                color2 = "Pink";            else                color2 = "LightBlue";            addlv(color1, color2, textBox_name.Text, comboBox1.Text, textBox_addr.Text);//添加一行        }        /******************  删除行  *********************/        //“清除选定行”按钮,清除选择的行        private void button_DelRow_Click(object sender, EventArgs e)        {            if (lv.SelectedItems.Count > 0)//判断lv有被选中项            {                lv.Items.Remove(lv.SelectedItems[0]);   //按项移除                //lv.Items.RemoveAt(lv.SelectedItems[0].Index); //按索引移除,与上一条语句作用一样            }            /*下面语句与上面等价            if (lv.SelectedItems.Count > 0)//判断lv有被选中项            {                int Index = lv.SelectedItems[0].Index;//取当前选中项的index,SelectedItems[0]这必须为0                lv.Items[Index].Remove();            }             */        }        /******************  删除所有行  *********************/        //“清除所有”按钮,清除所有行        private void button_DelAllRow_Click(object sender, EventArgs e)        {            lv.Items.Clear();   //清除所有项,不包括标题栏            //lv.Clear();       //清除标题栏及所有项        }        //点击“添加项目”按钮,使“添加项目”组框可见        private void button_add_Click(object sender, EventArgs e)        {            groupBox_add.Visible = true;        }        //“取消”按钮,使“添加项目”组框隐藏        private void button_cancel_Click(object sender, EventArgs e)        {            textBox_name.Text = "";            comboBox1.Text = "";            textBox_addr.Text = "";            groupBox_add.Visible = false;        }        //修改指定行指定列的内容        private void modify(int row, int col, string text)        {            if (row > lv.Items.Count || col > 4)                return;            if(row < 1) row = 1;            if(col < 1) col=1;            lv.Items[row - 1].SubItems[col - 1].Text = text;            //相当于  行索引           列索引        }        //插入行        private void InsertRow(int row,string name,string sex,string addr)        {            if (row > lv.Items.Count)                row = lv.Items.Count;            ListViewItem i_item = new ListViewItem();            //添加行的第一列内容            i_item.SubItems[0].Text = row.ToString();  //ID            //添加行的其他列内容            i_item.SubItems.Add(name);      //姓名            i_item.SubItems.Add(sex);       //性别            i_item.SubItems.Add(addr);       //籍贯            lv.Items.Insert(row-1, i_item);        }        /*************************** 删除选中多行 ***********************/        //删除多行        private void button_DelMulRow_Click(object sender, EventArgs e)        {            if (lv.SelectedItems.Count > 0)            {                int n = lv.SelectedItems.Count;                ListView.SelectedListViewItemCollection Sel_items = lv.SelectedItems;                for (int i = 0; i < n; i++)                {                    Sel_items[0].Remove();                }            }        }         /*************************** 右键删除选中行 ***********************/        //需先添加控件contextMenuStrip(此例名为contextMenuStrip1),然后找到ListView控件的contextMenuStrip属性,设置为contextMenuStrip1        //添加删除条目后,双击删除,编写代码如下        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)        {            if (lv.SelectedItems.Count > 0)//判断lv有被选中项            {                lv.Items.Remove(lv.SelectedItems[0]);                //lv.Items.RemoveAt(lv.SelectedItems[0].Index); //按索引移除,与上一条语句作用一样            }        }    }}

生成表格:
这里写图片描述

0 0
原创粉丝点击