对ListBox项进行排序以在网格视图中生成标题

本文关键字:视图 网格 标题 ListBox 排序 | 更新日期: 2023-09-27 18:19:48

我将参考此链接。如何使ListBox中的项目成为GridView 的标题

给出上面的链接具有如何使列表框项目成为网格视图的头文本的功能。我的问题是,如果我有一个2列表框。第一个是由数字组成,第二个是名称/单词。如何根据listbox1上的数字对第二个listbox进行排序,然后使输出成为gridview的标题?。

LBox1 LBox2

3 AAA

2 DDD

1 EEE

5 BBB

4 CCC

GridView标题应为

EEE DDD AAA CCC BBB。很高兴您能提供示例代码。谢谢

对ListBox项进行排序以在网格视图中生成标题

首先必须创建一个SortedDictionary。这个collction会根据关键字自动排序。

 SortedDictionary<string, string> values = new SortedDictionary<string, string>();

然后使用您的两个列表填充数据

for (int i = 0; i < ListBoxnumbers.Items.Count; i++)
        {
            values.Add(ListBoxnumbers.Items[i].ToString(), ListBox1.Items[i].ToString());
        }

之后,另一个循环将填充标题

 foreach (KeyValuePair<string,string> item in values)
        {
            dt.Columns.Add(item.Value, System.Type.GetType("System.String"));
        }