DataGridView event before DataSource changes

Is it possible to somehow trigger the DataSourceChanging event in a DataGridView. The DataGridView has a DataSourceChanged event, which (I believe) after binding the DataSource to the DataGridView. I want to do some things before the property is changed.

Sample code of my ...

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 such things in many forms. Just consider developing a general procedure that does this whenever the data source changes. The recovery part can be performed using the DataSourceChanged event ... But what event should I process to perform the save?

+3
source share
2 answers

I did not do this myself, but the DataGridView is not sealed, so you can create a new class that inherits it. Create a new “DataSourceChanging” event, then override the Setter DataSource property so that it will raise the event first and then actually set the property in the parent class.

Instead, you simply use this datagridview instead of the standard one, and connect your save logic to DataSourceChanging.

+5
source

You can create the shadow / new property in a subclass using new / shadow keywords, depending on whether you use C # or vb.net, since you cannot override it. Use the base.DataSource calls in a subclass to access the base class property and use this .DataSource in the subclass to access the new DataSource property .: D

+2
source

Source: https://habr.com/ru/post/1486011/


All Articles