C# grid DataSource polymorphism

前端 未结 7 1771
我在风中等你
我在风中等你 2021-01-06 06:28

I have a grid, and I\'m setting the DataSource to a List. What I want is to have the list bind to the underlying type, and disply

7条回答
  •  醉酒成梦
    2021-01-06 06:40

    My suggestion would be to dynamically create the columns in the grid for the extra properties and create either a function in IListItem that gives a list of available columns - or use object inspection to identify the columns available for the type.

    The GUI would then be much more generic, and you would not have as much UI control over the extra columns - but they would be dynamic.

    Non-checked/compiled 'psuedo code';

    public interface IListItem
    {
        IList ExtraProperties;
    
    
        ... your old code.
    }
    
    public class User : IListItem
    {
       .. your old code
        public IList ExtraProperties { return new List { "UserSpecificField" } }
    }
    

    and in form loading

    foreach(string columnName in firstListItem.ExtraProperties)
    {
         dataGridView.Columns.Add(new DataGridViewTextBoxColumn { DataPropertyName = columnName, HeaderText = columnName );
    }
    

提交回复
热议问题