把属性View设置为Details,确保列可以显示出来
this.listView1.View = System.Windows.Forms.View.Details;
属性Columns根据需要添加
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { this.columnHeader1, this.columnHeader2});
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader1.Text = "客户端地址"; this.columnHeader1.Width = 200;
this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader2.Text = "上线时间"; this.columnHeader2.Width = 200;
添加
ListViewItem listViewItem = new ListViewItem(new[] { e.IpEndPoint.ToString(), e.DateTime.ToString("yyyy-MM-dd HH:mm:ss.fff") }) {Name = e.Id.ToString()}; listView1.Invoke((MethodInvoker)delegate { listView1.Items.Add(listViewItem); });
移除
string name = e.Id.ToString(); listView1.Invoke((MethodInvoker) delegate { if (listView1.Items.ContainsKey(name)) { listView1.Items.RemoveByKey(name); } });