1

外側のリストがList<List<string>>グリッドの行になり、内側のリストが列の値になる場所があります。

またはList<List<string>>を受け入れるグリッドに適したデータソースになるようにラップするにはどうすればよいですか?IListIBindingList

事実上、バインディングのパブリックプロパティとして文字列を公開List<MyObject>しているクラスであると見なされるようにします。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;
    }
4

1 に答える 1

1

DataGridViewのデータソースを設定する場合、DataGridViewは、提供されたオブジェクトをとして扱いますIList<something>。それぞれについて、somethingリフレクションを使用して、すべてのパブリック読み取り可能なプロパティを検索します。aのパブリック読み取り可能なプロパティは、List<string>CapacityとCountです。

文字列をDataGridViewに表示するには、文字列をプロパティとして表示する必要があります。これは、少なくとも3つの方法で実行できます。独自のクラスを作成する(既に行ったようにSupplierEntry)、を使用するTuple、または匿名タイプを使用します。

データをコピーせずにソースを使用できるようにする妥協案List<List<string>>は、データをプロパティとして提示するだけのラッパークラスを提供することです。

// 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;
}
于 2012-11-08T22:21:02.713 に答える