2012-12-26 21 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"; 
} 

然后我要显示详细信息对象的集合中的一个DataGridView,使用下面

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

dgv.Columns.Add(column); 
代码

该列显示在网格中,但没有值

+0

你确定DataPropertyName是你想要什么?看起来你正在寻找更多的价值。我怀疑你不与ASP.Net,这是我最熟悉的工作,但我会检查DataGridViewTextBoxColumn的其他属性,看看是否有别的东西出现,这将使你所需要的。 – Melanie

+0

@Melanie我正在使用Windows窗体。我会看看其他属性 – Andrey

+0

另外,当我使用另一个对象的属性它的罚款,如:DataPropertyName =“MasterName” – Andrey

回答

2

您可以在实体子例如重写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; 
    } 

然后绑定实体的子名称。

+1

EF生成的课程怎么样?我无法修改它们 – Andiana

+0

那么我怎样才能让数据网格可以编辑DescripcionFormulario? – miguelmpn

0

其中是您的datasoure?你必须导致来源。它会找到它。 你可以做到这一点

来源:

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

如何获取此子类型? – miguelmpn

4

如果你要使用许多子元素是这样的:

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

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

如何:

1解决方案 设置每个细胞在某些事件值(mabye另一个是更好) ,手动设置数据源后,例如:

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

第二个解决方案 怎么样从数据创建数据表正确,然后只将它绑定?

我很感谢任何意见;-)

+1

好主意,并且可能很快。 MSDN警告说这个函数会经常被调用,应该避免冗长的处理。因此有两个改进:(1)只格式化通过参数e请求的单元格。该函数将被格式化的每个单元格调用一次。 (2)而不是使用字符串的列,使用e.ColumnIndex来访问该单元格 –

5

如果需要(即使用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; 
    } 
} 
1

只是这样做是更通用:

使用[Browsable(false)]掩盖您希望获取childvalue的属性,以便它不会显示在数据网格中。 然后创建你的类,它持有该只显示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;