开发者

looking for an explanation of .NET DataTable events and handling

开发者 https://www.devze.com 2023-03-11 12:11 出处:网络
I\'m having trouble wrapping my head around .NET DataTable events, handling, actions, etc. I have attempted to understand from the MSDN library, but I find I haven\'t got an understanding of how it

I'm having trouble wrapping my head around .NET DataTable events, handling, actions, etc.

I have attempted to understand from the MSDN library, but I find I haven't got an understanding of how it all works together. I also haven't been able to find any other source (by googling) that explains the ins and outs of it.

For example, I'm trying to understand that there are DataTable events (such as ColumnChanged, RowChanged, etc.) but there are also DataRowColumnChangeEventArgs, DataColumnChangeEventArgs, etc开发者_如何转开发. How do they relate/work together?

Can anyone provide a link that provides a thorough explanation of DataTable events and handling? (Or provide one yourself if you have that kind of time lol!) (Hoping for examples in C++ in particular, but if in another language I can make do for now)

Thanks so much!


It looks like you're getting confused between the events themselves and the EventArgs parameters that get passed to your actual event handler.

When you register to handle an event, there are a couple of things you have to know. I'll use your ColumnChanged event as an example.

The first is that the Event you're registering for is the ColumnChanged event (seems straight forward, but I included it for clarities sake).

The second is that to register for an event, you need an event handler. That event handler has to match the delegate signature provided by the event. In the case of ColumnChanged, the delegate must take two parameters: 1) an object called sender and 2) an object used to pass event specific arguments called DataColumnChangedEventArgs. Your method should look something like:

public void ColumnChangedHandler(object sender, DataColumnChangedEventArgs e)
{
    // Do some work here when the event is fired
}

After you have that, the last step is actually registering your handler to handle the event:

someDataTable.ColumnChanged += 
    new DataColumnChangeEventHandler(ColumnChangedHandler);

Now, if you look around you'll probably notice that some people use shortcut syntax to create a handler all in one step:

someDataTable.ColumnChanged += (object sender, DataColumnChangedEventArgs e)
    {
        // Do some work here when the event is fired
    }

That will allow you to create an anonymous method to handle the event, but could cause problems when it comes time to clean up the object and un-register any handlers.

0

精彩评论

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