C#从事件中更新Gridview列

时间:2017-08-22 13:50:59

标签: c#

我有一个Windows窗体,上面有一个GridView,以及以下属性

如果在表单初始化我输入

myGridView1.Columns.Add("ColumnName", "ColumnHeaderText");

当表单加载时我看到我的GridView上的列但是如果我在ListChanged事件中键入相同的代码我在GridView上看不到该列 为什么会这样?

private void _browseLayoutColumns_ListChanged(BrowseLayoutColumns item)
    {
        myGridView1.Columns.Add("ColumnName", "ColumnHeaderText");
    }

private MyList<BrowseLayoutColumns> _browseLayoutColumns = new MyList<BrowseLayoutColumns>();                                     
    public MyList<BrowseLayoutColumns> BrowseLayoutColumns
    {
        get 
        {
              return _browseLayoutColumns;
        }
        set
        {              
             if(value == null) return;                 
            _browseLayoutColumns = value;                                
        }            
    }

MyList和BrowseLayoutColumns实现如下:

public class BrowseLayoutColumns
{           
    #region Properties
    private string _columnName = string.Empty;        
    public string ColumnName
    {
        get => _columnName;
        set
        {
            if (null == value) return;
            _columnName = value;                
        }

    }

    private string _bindingField = string.Empty;
    public string BindingField
    {
        get => _bindingField;
        set
        {
            if (null == value) return;
            _bindingField = value;               
        }
    }       
    #endregion       
}

internal class MyList<T> : List<T> where T : class
{
    #region ListMethods
    public new void Add(T item)
    {
        base.Add(item);            
        ListChanged?.Invoke();
    }

    public new void Clear()
    {
        base.Clear();        
        ListChanged?.Invoke();
    }
    #endregion

    #region Events

    public event ListChangedEventHandler ListChanged;
    public delegate void ListChangedEventHandler();

    #endregion
}