using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace DataApp { public partial class ListViewFrm : Form { public ListViewFrm() { InitializeComponent(); } private void ListViewFrm_Load(object sender, EventArgs e) { BindListView(); } private void BindListView() { //显示方式一定要先设置 lvwShow.View = View.Details; lvwShow.GridLines = true; //表头,这里设置三列 ColumnHeader title; title = new ColumnHeader(); title.Text = "列0"; title.Width = 120; this.lvwShow.Columns.Add(title); title = new ColumnHeader(); title.Text = "列1"; this.lvwShow.Columns.Add(title); title = new ColumnHeader(); title.Text = "列2"; this.lvwShow.Columns.Add(title); //ListViewItem相当于行 ListViewItem lvwItem; //行中列值 ListViewItem.ListViewSubItem lvwSubItem; //第一行 lvwItem = new ListViewItem(); //Text属性相当于行标题 lvwItem.Text = "行头1"; //行中列值 lvwSubItem = new ListViewItem.ListViewSubItem(); lvwSubItem.Text = "坐标(1,1)"; lvwItem.SubItems.Add(lvwSubItem); lvwSubItem = new ListViewItem.ListViewSubItem(); lvwSubItem.Text = "坐标(1,2)"; lvwItem.SubItems.Add(lvwSubItem); //增加一行 lvwShow.Items.Add(lvwItem); //第二行 lvwItem = new ListViewItem(); //Text属性相当于行标题 lvwItem.Text = "行头2"; //行中列值 lvwSubItem = new ListViewItem.ListViewSubItem(); lvwSubItem.Text = "坐标(2,1)"; lvwItem.SubItems.Add(lvwSubItem); lvwSubItem = new ListViewItem.ListViewSubItem(); lvwSubItem.Text = "坐标(2,2)"; lvwItem.SubItems.Add(lvwSubItem); //增加一行 lvwShow.Items.Add(lvwItem); } private void lvwShow_Click(object sender, EventArgs e) { if (lvwShow.SelectedItems != null) { MessageBox.Show(lvwShow.SelectedItems[0].SubItems[1].Text.ToString()); } } private void btnDelete_Click(object sender, EventArgs e) { if (lvwShow.SelectedItems != null) { lvwShow.SelectedItems[0].Remove(); } } } }