13

次のクラスがあることを想像してみましょう

public class Master
{
    public string MasterName = "Something";

    public List<Detail> details = new List<Detail>();
}

public class Detail 
{
    public string Foo = "Test";
}

次に、以下のコードを使用して、DataGridView に Details オブジェクトのコレクションを表示します。

DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "Details.Foo";
column.HeaderText = "Foo header";

dgv.Columns.Add(column);

列はグリッドに表示されますが、値はありません

4

6 に答える 6

14

より一般的にする必要がある場合(つまり、を使用DataPropertyName = "MyProp1.MyProp2.MyProp3"する場合)、これを使用できます

private void Grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    DataGridViewColumn column = Grid.Columns[e.ColumnIndex];
    if (column.DataPropertyName.Contains("."))
    {
        object data = Grid.Rows[e.RowIndex].DataBoundItem;
        string[] properties = column.DataPropertyName.Split('.');
        for (int i = 0; i < properties.Length && data != null; i++)
            data = data.GetType().GetProperty(properties[i]).GetValue(data);
        Grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = data;
    }
}
于 2016-03-08T09:05:26.523 に答える
4

たとえば、エンティティの子で ToString メソッドをオーバーライドできます。

public class FormulariosENT {

    #region PROPERTIES

    public int IdFromulario { get; set; }
    public string DescripcionFormulario { get; set; }

    #endregion

    #region PUBLIC METHODS
    public override string ToString() {

        return DescripcionFormulario;
    }

その後、エンティティの子の名前をバインドします。

于 2013-04-18T01:00:46.333 に答える
4

これを行うだけです:

childvalue を取得するプロパティを [Browsable(false)] でマスクして、データグリッドに表示されないようにします。次に、childproperty 値を示す「get」メソッドのみを持つ子オブジェクトを保持するクラスに新しいプロパティを作成します。次に例を示します。

[Browsable(false)] //Because we use the CreatorUsernameProperty to do this.
public virtual User Creator { get; set; }

[DisplayName("Creator")] //shows like this in the grid
public string CreatorUsername => Creator?.Username;
于 2017-08-05T13:27:01.807 に答える
4

次のように多くの子要素を使用する場合:

class MyClass
{
   public int Id;
   public MyOtherClass OtherClass;
}

class MyOtherClass
{
   public string Name;
   public int Number;
}

どうですか:

最初の解決策 データソースを設定した後、手動で、あるイベントで各セルの値を設定します(おそらく別のセルの方が良いでしょう)。次に例を示します。

private void dgv_CellFormatting( object sender, DataGridViewCellFormattingEventArgs e )
{
   MyClass data = dgv.Rows[ e.RowIndex ].DataBoundItem as MyClass;

   dgv.Rows[ e.RowIndex ].Cells[ "colName" ].Value = data.OtherClass.Name;
   dgv.Rows[ e.RowIndex ].Cells[ "colNumber" ].Value = data.OtherClass.Number;
}

2番目 の解決策データから適切なDataTableを作成してからバインドするのはどうですか?

私はどんな意見にも感謝します;-)

于 2015-10-07T10:02:34.663 に答える
0

あなたのデータソースはどこですか?ソースにつながる必要があります。それを見つけるだろうと。あなたはこれを行うことができます

ソース :

  1. :

    List<Detail> list = new List<Detail>();
    for (int i = 0; i < 10; i++)
    {
         Detail d = new Detail();
         d.Foo = "test";
         list.Add(d);
    }
    
    this.dgv.DataSource = list;
    this.dgv.Columns[0].Visible = false;
    DataGridViewTextBoxColumn dgvc = new DataGridViewTextBoxColumn();
    dgvc.HeaderText = "列标题";
    dgvc.DataPropertyName = "foo";
    this.dgv.Columns.Add(dgvc);
    
  2. :

    public class Detail
    {
         private string foo;
    
         public string Foo
         {
             get { return foo; }
             set { foo = value; }
         }
     }
    
于 2012-12-27T03:32:13.343 に答える
0

bubiの答えは素晴らしいです。BindingListViewを使用している場合にアプローチが機能するように、小さな調整を追加しました。

編集:このアプローチは、サブプロパティを介して配置された列の並べ替えを無効にします。現時点でこれを修正する方法がわかりません。

if (column.DataPropertyName.Contains("."))
{
    object data = dgv.Rows[e.RowIndex].DataBoundItem;
    if (data is ICustomTypeDescriptor ictd) // support BindingListView
        data = ictd.GetPropertyOwner(null);
    string[] properties = column.DataPropertyName.Split('.');
    for (int i = 0; i < properties.Length && data != null; i++)
    data = data.GetType().GetProperty(properties[i])?.GetValue(data);
    e.Value = data;
}
于 2019-05-22T15:10:42.210 に答える