2012-12-26 20 views
7

を表示するには、私は次のクラスのDataGridView - 利用DataPropertyName子要素のプロパティ

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

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

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

を持っている。そして、私は

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

dgv.Columns.Add(column); 
次のコードを使用して、DataGridViewの中で詳細オブジェクトのコレクションを表示したいというイメージができます

カラムはグリッドに表示されますが、値なし

+0

あなたはDataPropertyNameが何をしたいですか?あなたはValueのラインに沿って何かを探しているようです。私はあなたがASP.Netで作業していないと思っています。これは私がよく知っていることですが、DataGridViewTextBoxColumnの他のプロパティを調べて、必要なものがあるかどうか確認します。 – Melanie

+0

@Melanie私はWindowsフォームを使用しています。私は他のプロパティを見てみる – Andrey

+0

また、別のオブジェクトのプロパティを使用すると、そのような:DataPropertyName = "MasterName" – Andrey

答えて

2

あなたはこの

ソースを行うことができます。

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; 
    } 

さらに、エンティティの子の名前をバインドします。

+1

EFで生成されたクラスはどうですか?私はそれらを変更することはできません – Andiana

+0

次に、DataGridでDescripcionFormularioを編集可能にする方法はありますか? – miguelmpn

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. :あなたは例えば、エンティティの子にToStringメソッドをオーバーライドすることができ

    public class Detail 
    { 
        private string foo; 
    
        public string Foo 
        { 
         get { return foo; } 
         set { foo = value; } 
        } 
    } 
    
+0

サブタイプの取得方法? – miguelmpn

4

あなたはこのような多くの子要素を使用したい場合:

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

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

方法について:いくつかのイベントでは各セルの

第一ソリューション 設定値を(mabye別の1が優れています)手動でデータソースを設定した後、たとえば:

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を作成してバインドするのはどうですか?

:あなたは(DataPropertyName = "MyProp1.MyProp2.MyProp3"を使用してIE)より汎用的にする必要がある場合

私はあなたがこの

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; 
    } 
} 
+1

良いアイデア、おそらく高速です。 MSDNは、この関数がかなり頻繁に呼び出されることを警告し、長い処理は避けるべきです。したがって、2つの改善点があります。(1)パラメータeを介して要求されたセルのみをフォーマットします。関数は、フォーマットされているすべてのセルに対して1回呼び出されます。 (2)列に文字列を使用する代わりに、e.ColumnIndexを使用してセルにアクセスします –

5

に感謝するだろう子値を取得するプロパティを[Browsable(false)]でマスクして、DataGridに表示されないようにします。たとえば : はその後childproperty値を示すだけで、「取得」メソッドを持っている子オブジェクトを保持している、あなたのクラスの新しいプロパティを作成

[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; 
1

ちょうどこれを行うに使用することができます任意の意見;-)

関連する問題