简体   繁体   中英

DataGridView Event Before DataSource Changes

Is there someway I could trigger DataSourceChanging event in DataGridView. The DataGridView has DataSourceChanged event which (I believe) after DataSource is bound to the DataGridView. I want to do some stuff before the property gets changed.

A Sample code of mine...

private void LoadGrid()
{
    //  I do some things like saving user settings here
    DtgRefundAssign.DataSource = BLL.GetDataSource(parameter1, parameter2); //Just to illustrate
    //  And restore them after the datasource is bound

}

I need to do similar stuffs in many forms. Just thinking to develop a common procedure which does this, whenever the datasource is changed. The restoring part can be done using DataSourceChanged event... But which event should I handle to do the saving part ?

I haven't done this myself, but DataGridView isn't sealed so you should be able to create a new class that inherits from it. Create a new event "DataSourceChanging", then override the DataSource property's Setter so that it first raises that event, then actually sets the property on the parent class.

You'd then simply use that datagridview in place of the default one, and hook up your save logic to DataSourceChanging.

You can create a shadows/new property in the sub class using new/shadows keywords depending on whether you use c# or vb.net, since you cant override it. Use base.DataSource calls in the sub class to access the base class's property and use this.DataSource in the sub class to access the new DataSource property. :D

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