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
ataTable
{
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.