Convert ColumnMapping in SubClass?

L

lucius

I would like to make a subclass of System.Data.DataTable with this
method pseudocode:

" for each DataColumn, change the ColumnMapping to
MappingType.Attribute "

However, I am not sure where I should put this kind of logic so it is
only executed once. The DataTable may be populated from a DataAdapter
fill, manually with NewRow(), or manually with Rows.Add( new
object[].. )

Should it occur when the DataTable's data is first "read" by another
instance?

Thanks.
 
W

WenYuan Wang [MSFT]

Hello Lucius,

According to your description, you want to change each column's
ColumnMapping to Attribute (in a subclass which inherits DataTable). If I
misunderstand, please correct me. Thanks.

Actually, each column is inserted by DataTable.Column.Add() method. For
DBDataAdapter, in its fill method, it will insert columns into datatabe
first, and then insert each datarow. Thus, if we want to change the
MappingType of each Column and only executed once, we should do something
with DataColumnCollection class. However, this class has been declared as
"sealed". This means it isn't possible for us to make a subclass from
DataColumnCollection and override its Add() method.

Fortunately, the DataColumnsCollection has a CollectionChanged event that
captures the Action (Add, Remove). Therefore, we could catch each ADD event
and change the current column as Attribute(MappingType). This event fires
when new column is added, which means, it will only be captured once for
each column. I think this is what you are looking for, isn't it?

For example, the code will looks as below:
class MyTable:DataTable
{
public MyTable():base()
{
this.Columns.CollectionChanged += new
System.ComponentModel.CollectionChangeEventHandler(Columns_CollectionChanged
);
}

void Columns_CollectionChanged(object sender,
System.ComponentModel.CollectionChangeEventArgs e)
{
if (e.Action ==
System.ComponentModel.CollectionChangeAction.Add)
{
DataColumn dc = e.Element as DataColumn;
dc.ColumnMapping = MappingType.Attribute;
}
}
}

Hope this helps.Please try this method and let me know if it works for you.
I'm glad to assist you.
Have a great day,
Sincerley,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

WenYuan Wang [MSFT]

Hello Lucius,

This is Wen Yuan, I just want to check if there is anything we can help
with.
Please feel free to update here if you meet any further issue on this.
We are glad to assist. thanks.

Have great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

WenYuan Wang [MSFT]

Thanks for your reply. Lucius.
You are welcome.

Have a nice day,
Sincerley,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top