简体   繁体   English

排序列表框项以在gridview中创建标题

[英]Sorting ListBox Items to make header in gridview

I will refer to this link. 我将参考此链接。 How do I make items in the ListBox as headers of GridView 如何在ListBox中使项目作为GridView的标题

giving the link above have the functionality on how to make a list box items be the headertext of a gridview. 提供上面的链接具有有关如何使列表框项成为gridview的标题文本的功能。 . My question would be. 我的问题是。 If I have a 2 List box. 如果我有一个2列表框。 first is consist of numbers, second is name/word. 第一个是数字,第二个是名称/单词。 How can I sort the second listbox based on the number on listbox1 then make the output be the header of gridview?. 如何根据listbox1上的数字对第二个listbox进行排序,然后使输出成为gridview的标题?

LBox1 LBox2 LBox1 LBox2

3 AAA 3 AAA

2 DDD 2 DDD

1 EEE 1 EEE

5 BBB 5 BBB

4 CCC 4 CCC

GridView Header should be GridView标头应为

EEE DDD AAA CCC BBB. EEE DDD AAA CCC BBB。 Glad if you could provide sample code.Thanks! 很高兴能提供示例代码。谢谢!

First you have to create a SortedDictionary. 首先,您必须创建一个SortedDictionary。 this collction automatically sort according to the key. 此集合将根据键自动排序。

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

And then fill data using your two lists 然后使用两个列表填充数据

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

after that another loop will fill the headers 之后,另一个循环将填充标题

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM