ListView用法
winform中ListView的用法
首先定义字头: this.listView1.Clear();
ColumnHeader ch = new ColumnHeader();
ch.Text = "URL";
ch.Width = 200;
ColumnHeader ch1 = new ColumnHeader();
ch1.Text = "QQ";
ch1.Width = 150;
ColumnHeader ch2 = new ColumnHeader();
ch2.Text = "Email";
ch2.Width = 200;
ColumnHeader ch3 = new ColumnHeader();
ch2.Width = 200;
将每列的开头绑定到ListView控件里面
listView1.Columns.AddRange(new ColumnHeader[] { ch, ch1, ch2, ch3 });
listView1.View = View.Details;
然后将数据列表绑定到控件:
定义个WebSiteInfo对象,里面包含WebSite,QQ,Email三个属性。
List<WebSiteInfo> list = new List<WebSiteInfo>();
给list赋值,然后将list绑定到ListView控件
if (websiteinfo.Count() > 0)
{
foreach (var item in websiteinfo)
{
ListViewItem lvi = new ListViewItem(new string[] { item.WebSite, item.QQ, item.Email }, -1);
listView1.Items.Add(lvi);
}
}
ListView选中行:
选中了几行:int i=listView1.SelectedItems.Count;
以下都是只选中了一行,多行的
foreach (System.Windows.Forms.ListViewItem item in listView1.SelectedItems)
{
}
即可
选中的是第几行: int index =listView1.SelectedItems[0].Index;
选中行的内容:该行第n列的数据: string txt= listView1.SelectedItems[0].SubItems[n].Text;
删除选中的行:listView1.SelectedItems[0].Remove();