2010-01-29 70 views
1

我有一本我想在表單中顯示的字典。 最簡單的方法是什麼?c#:如何輕鬆顯示字典<string, int>?

最好我想展示一個控件,我可以使用int值進行排序。 我試過一個DataGridView,但沒有顯示出來,我必須做一些錯誤的...

代碼:

mDataGridView.DataSource = mWordCount; 
/*Where mWordCount is the Dictionary<string, int> but nothing shows up. (It's a forms-app, not a web-app)*/ 
+3

「,我一定在做錯事......)」 - 也許你可以發佈你的代碼給人們一個線索? – 2010-01-29 01:09:30

+0

對不起,當然我應該發佈我的代碼:) mDataGridView.DataSource = mWordCount; 其中mWordCount是詞典 但沒有顯示出來。 (這是一個表單應用程序,而不是一個web應用程序) – Led 2010-01-29 01:11:06

+0

嘗試'mWordCount.ToList()',看看會發生什麼。 – 2010-01-29 01:12:25

回答

6

嘗試mWordCount.ToList(),看看會發生什麼。

解釋是here

DataGridView的類支持 標準的Windows窗體數據綁定 模型。這意味着數據源可以 是實現的 一個以下接口的任何類型的:

  • IList接口,包括一維數組。
  • IListSource接口,如DataTableDataSet 類。
  • IBindingList接口,如BindingList<(Of <(T>)>) 類。
  • IBindingListView接口,如BindingSource類。
+0

作品像一個魅力,謝謝你! 你可以通過任何機會告訴我如何顯示按int值排序的數據網格嗎? – Led 2010-01-29 01:16:36

+0

'SortedColumn'屬性應該允許你這樣做:http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.sortedcolumn.aspx – 2010-01-29 01:18:05

相關問題