【问题标题】:Make List<List<string>> suitable for binding使 List<List<string>> 适合绑定
【发布时间】:2012-11-08 14:54:29
【问题描述】:

我有一个List&lt;List&lt;string&gt;&gt;,其中外部列表​​是网格的行,内部列表是列值。

如何包装 List&lt;List&lt;string&gt;&gt; 使其成为网格的合适数据源,接受 IListIBindingList

实际上,我希望它被视为List&lt;MyObject&gt;,其中MyObject 类将字符串公开为用于绑定的公共属性。

我无法更改列表,而且它可能包含大量行,因此复制数据并不理想。

差异的一个简单示例是以下代码,其中DataGridView 放在WinForm 上:

 public class SupplierEntry
    {
        public string SupplierCode
        {
            get
            {
                return "SUPPLIERCODE";
            }
        }

        public string SupplierTitle
        {
            get
            {
                return "SUPPLIERTITLE";
            }
        }
    }

    private void Test()
    {
        List<string> supplierEntryString = new List<string>();
        supplierEntryString.Add("SUPPLIERCODE");
        supplierEntryString.Add("SUPPLIERTITLE");            

        List<List<string>> supplierListStrings = new List<List<string>>();
        supplierListStrings.Add(supplierEntryString);

        List<SupplierEntry> supplierListObjects = new List<SupplierEntry>();

        SupplierEntry supplierEntryObject = new SupplierEntry();
        supplierListObjects.Add(supplierEntryObject);

        //this can't handle the nested collections, instead showing a Capacity and Count column 
        dataGridView1.DataSource = supplierListStrings;

        //this works giving you a supplier code and title column
        //dataGridView1.DataSource = supplierListObjects;
    }

【问题讨论】:

  • “网格”是指DataGridView吗?
  • Infragistics UltraGrid 和 UltraCombo(丢弃的部分实际上也是一个网格)。

标签: c# winforms data-binding .net-4.0 infragistics


【解决方案1】:

在设置 DataGridView 的 DataSource 时,DataGridView 会将提供的对象视为 IList&lt;something&gt;,其中对于每个 something,它将使用反射来查找所有公共可读属性。 List&lt;string&gt; 的公共可读属性是容量和计数。

为了让您的字符串出现在 DataGridView 中,它们必须作为属性呈现。您至少可以通过三种方式做到这一点:创建自己的类(正如您已经使用 SupplierEntry 所做的那样)、使用 Tuple 或使用匿名类型。

让您在不复制任何数据的情况下使用源 List&lt;List&lt;string&gt;&gt; 的折衷方案是提供一个仅将数据呈现为属性的包装类:

// Provide named properties which really just read elements 
// from the List<string> provided with the constructor
public class ListBasedRecord {
    public string SupplierName { get { return source[0]; } }
    public string SupplierCode { get { return source[1]; } }
    private List<string> source;
    public ListBasedRecord(List<string> source) { this.source = source; }
}

private void ListTest() {
    // ... same as above, you get your List<List<string>> ...
    // Succinctly create a SupplierEntryWrapper for each "record" in the source
    var wrapperList = supplierListStrings
                      .Select(x => new SupplierEntryWrapper(x)).ToList();
    dataGridView1.DataSource = wrapperList;
}

【讨论】:

  • 确认一下,这只是为每个字符串列表添加少量内存?
  • 您可以使用分析器进行检查,但每条记录应该只有几个字节。附加到每个ListBasedRecord 的唯一数据 是一个对象引用(私有字段source),它只占用4 个字节——相当于一个2 个字符的字符串。
猜你喜欢
  • 2013-12-07
  • 1970-01-01
  • 1970-01-01
  • 2014-01-31
  • 1970-01-01
  • 2011-02-10
  • 2013-09-05
  • 2012-12-19
  • 1970-01-01
相关资源
最近更新 更多