简体   繁体   English

DataSource更改之前的DataGridView事件

[英]DataGridView Event Before DataSource Changes

Is there someway I could trigger DataSourceChanging event in DataGridView. 有没有我可以在DataGridView中触发DataSourceChanging事件。 The DataGridView has DataSourceChanged event which (I believe) after DataSource is bound to the DataGridView. DataGridView具有DataSourceChanged事件(我相信)在DataSource绑定到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 ? 恢复部分可以使用DataSourceChanged事件完成...但是我应该处理哪个事件来执行保存部分?

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. 我自己没有这样做,但DataGridView没有密封,所以你应该能够创建一个继承它的新类。 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. 创建一个新的事件“DataSourceChanging”,然后覆盖DataSource属性的Setter,以便它首先引发该事件, 然后实际设置父类的属性。

You'd then simply use that datagridview in place of the default one, and hook up your save logic to DataSourceChanging. 然后,您只需使用datagridview代替默认值,并将保存逻辑连接到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. 您可以使用new / shadows关键字在子类中创建阴影/新属性,具体取决于您是使用c#还是vb.net,因为您无法覆盖它。 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. 在子类中使用base.DataSource调用来访问基类的属性,并在子类中使用this.DataSource来访问新的DataSource属性。 :D :d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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