views:

812

answers:

3

I have two datatables. First is

DataTable NameAdressPhones = new DataTable(); 

with Three columns Name,Adress and PhoneNo.But I want only two columns Name and Adress data so I am copy those columns (with data) to the new datatable

DataTable NameAdress = new DataTable(); 

For that I do

            foreach (DataRow sourcerow in NameAdressPhones.Rows)
            {
                DataRow destRow = NameAdress.NewRow();
                foreach (string colname in columns)
                {
                    destRow[colname] = sourcerow[colname];
                }
                NameAdress.Rows.Add(destRow);
            }

Now I clear every time the NameAdressPhones(first) datatable when there are new records in the table.And every time there will be same no of columns but column name will be different like Nm instead of Name,Add instead of Address.Now problem is second datatable have already column names Name and Address and now I want to copy the columns data of Nm and Add to the second datatabel but the column names are different to the second datatable.So even If there is different column names I want to copy Nm column data of first datatable to the column Name of second datatable and column Add data of first datatable to column Address of second datatable.

In short how can we copy column data from one datatable to another even if there is different column names of both datatable like Nm is the column name of first datatable and Name is the column name of second datatable then the data of the column Nm should be copied to the column Name.

+3  A: 

Here's the simplest way:

foreach (DataRow sourcerow in NameAdressPhones.Rows)
{
    DataRow destRow = NameAdress.NewRow();
    destRow["Name"] = sourcerow["Nm"];
    destRow["Address"] = sourcerow["Add"];
    NameAdress.Rows.Add(destRow);
}

Automation is great when it's available. When it's not, you have to map source columns to destination columns in some manner.

If the columns are in the same order in both tables, you could just reference the values by ordinal instead of column name, but that's such a bad idea I'm not even going to post any code for it.

MusiGenesis
@MusiGenesis,Thanks...Great..I have not just thought like that.But sir how can I map the columns ?
Harikrishna
If you use my code sample, that's the map. If you want to get fancy and maintain a bunch of lists somewhere of column mappings, you can, but you're probably just making more work for yourself.
MusiGenesis
@MusiGenesis,There is not fixed order of column every time.Like column Nm once may have index number of 0 then next time it may be at position 2 in the table.Then will it work ?
Harikrishna
@Harikrishna: rules are meant to be broken, but not this one. NEVER EVER EVER EVER EVER reference columns by their index, since column orderings in the underlying database (or in the SELECT query) can easily change.
MusiGenesis
@MusiGenesis,Does it mean reference the columns by its name then there will not be any problems ?
Harikrishna
A: 

If I've understood your question right, then the way this is usually done is by using stored procedures. You have the same stored procedures in both databases, but the implementation is specific to the table schema of each database. This allows you the abstraction you need.

Driss Zouak
A: 

Use column index number rather than names:

destRow[0] = sourcerow[0]; // for column 0 = "Name" or "NM"
SeaDrive
@SeaDrive, But there is not fixed index of the columns.Like column Nm will be at position 0 or not.
Harikrishna
You have to have some way of knowing which columns you want, i.e. something to keep you from putting the address in the name column. However you know that, it shouldn't be too hard to cope using either ordinals, or string variables as the index.n1 = "Name";n2 = "Nm" ;// laterdestRow[n1] = sourceRow[n2] ;
SeaDrive