Dynamic Auto updating (to UI, Grid) binding list in C# Winform?

Posted by Dhana on Stack Overflow See other posts from Stack Overflow or by Dhana
Published on 2012-11-16T07:14:20Z Indexed on 2012/11/20 5:00 UTC
Read the original article Hit count: 219

Filed under:
|
|

I'm not even sure if i'm doing this correctly. But basically I have a list of objects that are built out of a class/interface. From there, I am binding the list to a datagrid view that is on a Windows Form (C#)

Here the list is a Sync list which will auto update the UI, in this case datagridview.

Every thing works fine now, but now i would like to have the List should have an dynamic object, that is the object will have by default two static property (ID, Name), and at run time user will select remaining properties. These should be bind to the data grid. Any update on the list should be auto reflected in the grid.

I am aware that, we can use dynamic objects, but i would like to know , how to approach for solution,

datagridview.DataSource = myData;  // myData is AutoUpdateList<IPersonInfo> 

Now IPersonInfo is the type of object, need to add dynamic properties for this type at runtime.

 public class AutoUpdateList<T> : System.ComponentModel.BindingList<T>
{

    private System.ComponentModel.ISynchronizeInvoke _SyncObject;
    private System.Action<System.ComponentModel.ListChangedEventArgs> _FireEventAction;

    public AutoUpdateList()
        : this(null)
    {
    }

    public AutoUpdateList(System.ComponentModel.ISynchronizeInvoke syncObject)
    {

        _SyncObject = syncObject;
        _FireEventAction = FireEvent;
    }

    protected override void OnListChanged(System.ComponentModel.ListChangedEventArgs args)
    {
        try
        {
            if (_SyncObject == null)
            {
                FireEvent(args);
            }
            else
            {
                _SyncObject.Invoke(_FireEventAction, new object[] { args });
            }
        }
        catch (Exception)
        {
            // TODO: Log Here
        }
    }

    private void FireEvent(System.ComponentModel.ListChangedEventArgs args)
    { 
        base.OnListChanged(args);             
    }
}

Could you help out on this?

© Stack Overflow or respective owner

Related posts about c#

Related posts about data-binding