开发者

Wrapper for code block

开发者 https://www.devze.com 2023-02-11 16:47 出处:网络
For example i have a datagrid and want to do something with it before databinding and after databinding, like so:

For example i have a datagrid and want to do something with it before databinding and after databinding, like so:

dgvTasksDocList.SaveLayoutToFile();
sTATUSCHANGESEXTENDEDBindingSource.DataSource = dt;
dgvTasksDocList.RestoreLayoutFromFile();

And i want t开发者_如何学Co add such code with all that cases when binding some sources. Is there an easy way to write such code before/after some action(Sure i can add this 2 lines, but may be there is some known way of doing such things)?


Seems to be a case for AOP - http://en.wikipedia.org/wiki/Aspect-oriented_programming.

For example with PostSharp you will be able to create an attribute PersistLayout and apply it like this:

[PersistLayout]
public void BindDataSource(object dt)
{
    sTATUSCHANGESEXTENDEDBindingSource.DataSource = dt;
}

Then Postsharp will extent source-code at compile-time to invoke your additional two-lines.

Other idea might be to invoke these two methods in sTATUSCHANGESEXTENDEDBindingSource.DataSource property setter but this decision needs a lot more context to know.


public class MyDGV : DataGridView
{
    public MyDGV()
    {
        DataSourceChanged += (s, e) => SaveLayoutToFile();
        DataBindingComplete += (s, e) => RestoreLayoutFromFile();
    }
}


Made it just with extension method, so now it looks like:

dgvTasksDocList.BindSourceAndSaveLayouts(sTATUSCHANGESEXTENDEDBindingSource, dt);

Or even:

dgvTasksDocList.BindSourceAndSaveLayouts(dt);

Because dgvTasksDocList.DataSource can be casted to BindingSource(sure i MUST provide that BindingSource)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号