简体   繁体   中英

C# Updating Gridview columns from an event

I have a windows form and have a GridView on it, along with the following property

if on the forms Initialization I type

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

when the form loads I see the column on my GridView however if I type the same code inside the ListChanged event I don't see the column on my GridView why is this happening?

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 and BrowseLayoutColumns implementations are below:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM