c#字典dictionary绑定datagridview如何排序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#字典dictionary绑定datagridview如何排序相关的知识,希望对你有一定的参考价值。
double m;
Dictionary<int, double> dic = new Dictionary<int, double>();
m = 0.26531 * sim1 + 0.22449 * sim2 + 0.18367 * sim3 + 0.14286 *sim4 +0.10204 * sim5 + 0.06122 * sim6 + 0.0041 * sim7;
dic.Add(int.Parse(tl.GetValue(0).ToString()), m);
BindingSource bs = new BindingSource();
bs.DataSource = dic;
this.dataGridView1.DataSource = bs;
this.dataGridView1.Columns[0].HeaderText = "Gid";
this.dataGridView1.Columns[1].HeaderText = "相似度";
如何对“相似度”列进行排序??请高手指点!谢谢!
Dictionary排序
Dictionary<int, string> list = new Dictionary<int, string>();//实例化一个字典
Random rd = new Random();//为了更加真实,创建一个生成整数的随机数生成器
int id = 0;
List<int> idlist = new List<int>();//用来生成100个整数,并存放到列表里idlist
for (int i = 0; i < 100; i++)
id = rd.Next(1, 100);
idlist.Add(id);
//遍历列表idlist,并加入字典list
foreach (int item in idlist)
if (!list.ContainsKey(item))
list.Add(item, "字典排序测试");
//实现排序的关键方法,可以声明一个key-value结构的集合数组,这里用了List,只要符合key-value结构的数据结构都可以,
//然后利用字典的OrderBy(升序)、OrderByDescending(降序)方法轻松实现排序功能
//通过过linq也可以实现:var result = from pair in dic orderby pair.Key select pair
List<KeyValuePair<int, string>> lstorder = list.OrderByDescending(c => c.Key).ToList();
DataTable dt = new DataTable();
dt.Columns.Add("编号");
dt.Columns.Add("内容");
foreach (KeyValuePair<int, string> item in lstorder)
dt.Rows.Add(item.Key, item.Value);
this.dataGridView1.DataSource = dt;//用DataGridView来展示结果
效果如下
参考技术A Dictionary<int, double> dic = new Dictionary<int, double>();dic.OrderBy(t=>t.Value); // 顺序
或者dic.OrderByDescending(t=>t.Value); // 倒序
注:需要引用System.Linq命名空间。
C#字典 Dictionary 的使用
//初始化字典值 //方式1 Dictionary<int, string> dict = new Dictionary<int, string> { { 1, "Mohan" }, { 2, "Kishor" }, { 3, "Pankaj" }, { 4, "Jeetu" } }; //方式2 var dict = new Dictionary<string, int> { ["one"] = 1, ["two"] = 2, ["three"] = 3 };
2.字典的使用示例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Demo1 { class Program { static void Main(string[] args) { //创建泛型哈希表,Key类型为int,Value类型为string Dictionary<int, string> myDictionary = new Dictionary<int, string>(); //1.添加元素 myDictionary.Add(1, "a"); myDictionary.Add(2, "b"); myDictionary.Add(3, "c"); //2.删除元素 myDictionary.Remove(3); //3.假如不存在元素则添加元素 if (!myDictionary.ContainsKey(4)) { myDictionary.Add(4, "d"); } //4.显示容量和元素个数 Console.WriteLine("元素个数:{0}",myDictionary.Count); //5.通过key查找元素 if (myDictionary.ContainsKey(1)) { Console.WriteLine("key:{0},value:{1}","1", myDictionary[1]); Console.WriteLine(myDictionary[1]); } //6.通过KeyValuePair遍历元素 foreach (KeyValuePair<int,string>kvp in myDictionary) { Console.WriteLine("key={0},value={1}", kvp.Key, kvp.Value); } //7.得到哈希表键的集合 Dictionary<int, string>.KeyCollection keyCol = myDictionary.Keys; //遍历键的集合 foreach (int n in keyCol) { Console.WriteLine("key={0}", n); } //8.得到哈希表值的集合 Dictionary<int, string>.ValueCollection valCol = myDictionary.Values; //遍历值的集合 foreach( string s in valCol) { Console.WriteLine("value:{0}",s); } //9.使用TryGetValue方法获取指定键对应的值 string slove = string.Empty; if (myDictionary.TryGetValue(5, out slove)) { Console.WriteLine("查找结果:{0}", slove); } else { Console.WriteLine("查找失败"); } //10.清空哈希表 //myDictionary.Clear(); Console.ReadKey(); } } }
运行结果:
以上是关于c#字典dictionary绑定datagridview如何排序的主要内容,如果未能解决你的问题,请参考以下文章