1

Datagridview を StringCollection にバインドすることは可能ですか? ある方法でやってみました

    StringCollection dict = Settings.Default.MyDict;
    BindingSource bs = new BindingSource();
    bs.DataSource = dict;
    this.DGV.DataSource = bs;

コレクション datagridview のアイテムの代わりにつぼみは、アイテムの長さを示します。

4

1 に答える 1

2

問題は、それが にバインドするときStringCollection、基になる型がstringそうであるため、型から見つけた最初のプロパティを引き出してstring表示することです。そのプロパティは長さです。

あなたができることは、独自のクラスであなたをラップし、のテキスト StringCollectionを表示するプロパティを公開することです。string

のラッパー クラスstring:

public class MyString
{
    private string _myString;

    public string Text
    {
        get { return _myString; }
        set { _myString = value; }
    }

    public MyString(string str)
    {
        _myString = str;
    }
}

コードは次のようになります。

StringCollection dict = Settings.Default.MyDict; 
// put your string in the wrapper
List<MyString> anotherdict = new List<MyString>();
foreach (string str in dict)
{
    anotherdict.Add(new MyString(str));
}
BindingSource bs = new BindingSource();
// bind to the new wrapper class
bs.DataSource = anotherdict;
this.DGV.DataSource = bs; 
于 2012-04-13T15:10:01.217 に答える