Wire RowChang events in C# using partial classes (Easy in VB - Not

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Any advice on how to use partial classes in C# to subscribe to RowChanging or
ColumnChangine events????

http://msdn.microsoft.com/vbasic/default.aspx?pull=/library/en-us/dnvs05/html/newdtastvs05.asp
is an excellent VB.Net article on using partial classes to validate changes
to DataTable row events. However, this appears impossible using C#!!

VB supports the following approach:

Partial Public Class NorthwindDataSet Partial Class OrdersDataTable
Protected Sub ValidateNewRow(ByVal sender As Object, _ ByVal e As
System.Data.DataTableNewRowEventArgs) _ Handles Me.TableNewRow

C# does not permit wiring in this fashion…

One help article suggests that the event handlers are simply generated by
double-clicking on the table or field names… works great in VB… not in C#:
http://msdn2.microsoft.com/en-us/library/ms171896.aspx


http://msdn2.microsoft.com/en-us/library/1120xds5.aspx
suggests that you simply copy code into your
“Copy the following code into the RowChanging event handlerâ€
But gives not clue as to how you wire the event or execute ANY code in a C#
partial class.

The same article suggests using the well tried approach:
http://msdn2.microsoft.com/en-us/library/awbftdfh.aspx
This does not work due to an inability to call any method that permits
subscription to an event.
 
You could subscribe to events by using new EventHandler. That should work
with Partial classes also.
The problem arises when you have a class split in 10 partial classes, and
you have no easy way of finding where that event was subscribed - which is
why I'd recommend staying away from partial classes for anything other than
autogenerated code.

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
__________________________________________________________
 
Sahil's solution does not work because you must be within a method to create
a new instance of the EventHandler.... this is not possible within partial
classes that are auto-generated by Visual Studio 2005... there is no way for
ANY method in the DataSet classes to be executed. Constructor cannot be
overriden as duplicate name errors occur within partial classes. There is no
way to hook into any event without being within a method.

In C#, attempt to add and EXECUTE a new EventHandler... it is not possible?
 
Okay I also posted it on my blog and a fellow MVP (Matt) came around and
suggested a solution.

public override void EndInit()
{
base.EndInit();

// necessary so that OnTableNewRow is called!
TableNewRow += delegate(object sender, DataTableNewRowEventArgs e) { };
}

(And add the delegate the handles TableNewRow).

Ideally the OnTableNewRow override should ahve been called automatically,
but it isn't due to a bug. Please see
http://codebetter.com/blogs/sahil.malik/comments/135954.aspx for more
details.

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
__________________________________________________________
 
I have a standard typed dataset (.net 2005). And created a separete class
file to handle the RowChanging event. The dataset name is dalCadastro and the
datatable is pessoa. The code I wrote to extend the dataset and handle the
event is as follows:

public partial class dalCadastro
{
public partial class pessoaDataTable
{
public override void BeginInit()
{
base.BeginInit();
this.RowChanging +=
new DataRowChangeEventHandler(Validate);
}

void Validate(object sender, DataRowChangeEventArgs e)
{
throw new Exception("Can not change!");
}
}
}

See if it works for you.....
 
Back
Top