Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 308
The King Avatar asked Mar 03 '26 12:03

The King


1 Answers

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.

like image 165
Tridus Avatar answered Mar 06 '26 02:03

Tridus