c# winform listview显示数据
电脑版发表于:2019/6/20 14:10
这样一个小小的问题坑了我好久啊....................好多年没有用winfrom了,居然因为一个listview显示数据问题坑了那么那么久...
记录一下,防止下次被坑,虽然winfrom几乎没有用了
其实最代码都是写正确了的,就是因为一个小小的属性!没有写,数据死活没有显示出来!来看看这个属性是个什么妖怪
对就是这个listview显示模式问题!,几百年没有用了谁还记得这个鬼哇
下面贴一下listview显示键值对的代码,做一个记录吧,也许几百年不用后还能看到呢。
public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Width = 600; this.Height = 600; //添加表头 this.listView1.Columns.Add("Key", 65, HorizontalAlignment.Center); this.listView1.Columns.Add("击杀数", 65, HorizontalAlignment.Center); this.listView1.Columns.Add("当前阵营", 65, HorizontalAlignment.Center); this.listView1.Columns.Add("账户名称", 65, HorizontalAlignment.Center); listView1.View = View.Details; } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { //构建好键值对 Dictionary<string, UserInfo> diclist = new Dictionary<string, UserInfo>(); diclist.Add("One", new UserInfo() { kill = 500, area = "蓝", user = "聚心动E" }); diclist.Add("Two", new UserInfo() { kill = 200, area = "橙", user = "聚心动A" }); diclist.Add("Three", new UserInfo() { kill = 600, area = "蓝", user = "聚心动B" }); diclist.Add("Four", new UserInfo() { kill = 50, area = "橙", user = "聚心动C" }); //键值对排序 var dicSort = from objDic in diclist orderby objDic.Value.kill descending select objDic; //排序方法2 var discSort2 = diclist.OrderByDescending(a => a.Value.kill); //显示数据 foreach (var item in discSort2) { ListViewItem viewitem = new ListViewItem(item.Key); viewitem.SubItems.Add(item.Key); viewitem.SubItems.Add(item.Value.kill + ""); viewitem.SubItems.Add(item.Value.area); viewitem.SubItems.Add(item.Value.user); listView1.Items.Add(viewitem); } } }
效果如下: