2012-07-11 112 views
0

嘿,我有一些列屬性列表,包含像HeaderName等信息 而且我有一個Datagrid綁定到項目列表,列是像列屬性名單。通過列集合在C#中對列表進行排序

所以現在用戶可以調整可見列的位置和寬度。這可能只有30個可能的列中的3個。 但我想將列表的「排序」部分保存到舊列表中。

好吧,我hesre第一種方法:

var columns = grid.Columns.OrderBy(p => p.DisplayIndex); 

     var prop = DynamicManager.GetPropertyGridByTableNameGroupLanguage("Article", 1, 1); 
     var orderesProp = from o in columns 
          join i in prop 
          on o.Header.ToString() equals i.PropertyName 
          orderby o.DisplayIndex 
          select new TableProperty 
          { 
           DbPropertyType = i.DbPropertyType, 
           GroupingProperty = i.GroupingProperty, 
           Mandatory = i.Mandatory, 
           MandatoryDB = i.MandatoryDB, 
           MaxValue = i.MaxValue, 
           MaxValueDB = i.MaxValueDB, 
           MinValue = i.MinValue, 
           MinValueDB = i.MinValueDB, 
           PropertyImportCode = i.PropertyImportCode, 
           PropertyName = i.PropertyName, 
           ReadOnly = i.ReadOnly, 
           Searchable = i.Searchable, 
           UIPropertyName = i.UIPropertyName, 
           UIPropertyType = i.UIPropertyType, 
           Visible = i.Visible, 
           VisibleInGrid = i.VisibleInGrid, 
           Width = o.ActualWidth 
          }; 

當我第一次從欄目中選擇,我得到的名單正確排序,但只有31的3項。當我先選擇prop,然後加入列時,我仍然有完整的列表,但沒有通過displayindex正確排序。

你有什麼想法如何解決這個問題?

回答

0

如何:

var orderesProp = from i in prop 
          select new { i = i, o = columns.FirstOrDefault(x => i.Name == x.Name) } into merge 
          orderby merge.o == null ? int.MaxValue : merge.o.DisplayIndex 
          select merge.i; 
+0

嘿,感謝您的答案,但你的解決方案似乎並不爲我工作。它仍然不是正確的列表:/ – 2012-07-11 06:59:08

+0

你能澄清什麼是錯的?在我的示例應用程序中,我獲取了prop和list的所有元素,並以顯示順序中與可見列相對應的元素開頭,然後所有其他(不可見)元素休耕。 – Rafal 2012-07-11 07:14:45

+0

我按編號,代碼和名稱對網格進行排序,但在orderesProp中,排序的值不會排序。 甚至「不可見」的對象介於列表之間,所以它不是列索引第一,然後其餘的項目:( – 2012-07-11 08:43:28