I have a datarow from one table and trying to use that datarow to update the corresponding datarow in another datatable. I know I can loop thru and set each cell but was wondering if there is a sort merge functionality like there is datatables only just the individual datarow
If you have the indices of both of the rows you can just set the values of the two data row's item arrays to be equal to one another. For example:
DataRow firstTableRow = FirstTable.Rows[YourRow];
DataRow secondTableRow = SecondTable.Rows[YourOtherRow];
secondTableRow.ItemArray = firstTableRow.ItemArray;
Related
I'd like to know if there is a way to automatically add columns in a DataTable inside a foreach loop? That is what I mean with "automatically".
I had in mind something like this:
DataTable dt = new DataTable();
foreach (var item in model.Statistik)
{
dt.Columns.Add();
row = dt.NewRow();
row[//Name of column goes here] = // either item or item.property;
dt.Rows.Add(row);
}
This is much more efficient than explicitly name every column before the for each loop. Because, what happens if some property changes or gets deleted, etc.? This is therefore, something I wish to get rid of.
I don't think you understand how dt.Columns and dt.Rows are related. There is one set of Columns for a DataTable. Every Row in the DataTable has all the matching columns - you don't have unique Columns in each Row. So every time you do dt.Columns.Add you are adding a column to every Row, old or new.
Also, how do you expect Row[<name of column>] to work if you don't specify the name to the DataTable?
Its kind of subjective question to ask but still i hope i will find help.
I was learning about merging two tables from database to a singe DataTable.Then i came accross the following block of code.
DataTable mdt = new DataTable();
mdt.Columns.Add("products");
mdt.Columns.Add("price");
for (int i = 0; i < A.Rows.Count; i++)
{
DataRow dr = mdt.NewRow();
dr["product"] = A.Rows[i]["product"].ToString();
dr["price"] = A.Rows[i]["price"].ToString();
//A is a DataTable
mdt.Rows.Add(dr);
}
I can understand that the datas are being added to the row of a Datatable.
This is what i understood:
The column product of DataTable is assigned a value by dr["product"].Correct me if i am wrong.But how is this A.Rows[i]["product"].ToString(); working.
This should help:
A = DataTable
A.Rows = Collection of DataRows in A
A.Rows[i] = i-th row from collection
A.Rows[i]["product"] = Column "product" in row (return type of expression is object)
So when you do dr["product"] = A.Rows[i]["product"].ToString();, you are assigning the value of the product column of the current row from datatable A to the product column in your new data row. Similarly for the price column.
Rows[i] represents the index to which the value is assigned.I mean for the first loop the value is 0,for second loop the value is 1.So for the firs loop the values of product and price are added to first row with index 0.Similarly for the second loop the values of product and price are added to the second row with index 1.
And for the second part ie ["product"].ToString(),its simply converting the value of product to string.
EDIT
Since A is a Datatable which is already filled.What we are doing with that statement is,we are taking the DataTables's i-th row from the collection,converting it to string and assigning it to the column "product" in the datarow.
The ["product"].ToString() Is taking the value of the cell with the column name product, and converting it to a string to be assigned to your new row.
I am having one datatable to that table i want to pass value from another datatable.
for example:-
datatable1 in this
i want to pass value from
datatable2.row[0]["colname"]
i tried like :-
tblvdr.Rows[0]["item_Vendorname"] = dttemp.Rows[0]["item_Vendorname"];
error message:-
its showing error for No row at o position
DataRow tblvdrRow = tblvdr.NewRow();
tblvdrRow["item_Vendorname"]= dttemp.Rows[0]["item_Vendorname"];
tblvdr.Rows.Add(tblvdrRow);
It looks like your data table tblvrd does not have any row. For this use
DataRow newRow = tblvdr.NewRow();
One tme a new row is generated you can populate it from the row in another table. Naturally you need generate new row in tblvdr for every row in dttemp
i have a datable and like this i have searched a datarow from the datable on the basis of some primary now i want to add that searched row to another datatable how can i achieve this please let me know
DataTable findRows = (DataTable)ViewState["dt"];
List<int> selectedList=(List<int>)ViewState["selectedList"];
DataTable temp = new DataTable();
foreach (int id in selectedList)
{
DataRow dr=findRows.Rows.Find(id);
}
now i want it to add to datatable temp how can i achieve this?
First, when creating temp don't just instantiate it as a new DataTable but instead call .Clone() on findrows to create a structurally identical DataTable.
Second, use .ImportRow() on the second DataTable and pass it the row from the first DataTable that you'd like to copy. This should create an entirely new row in the second table with the same values as the row from the first table.
I have two DataTables.
First is
DataTable NameAddressPhones = new DataTable();
with Three columns Name, Address and PhoneNo.But I only want two columns Name and Address data so I want to copy those columns (with data) to the new DataTable.
DataTable NameAddress = new DataTable();
For that I do
foreach (DataRow sourcerow in NameAddressPhones.Rows)
{
DataRow destRow = NameAddress.NewRow();
foreach (string colname in columns)
{
destRow[colname] = sourcerow[colname];
}
NameAddress.Rows.Add(destRow);
}
I clear the NameAddressPhones(first) DataTable every time there are new records inserted in the table. And every time there will be the same number of columns but the column names will be different like Nm instead of Name, Add instead of Address.Now the problem is the second DataTable already has column names Name and Address and now I want to copy the columns data of Nm and Add to the second DataTable but the column names are different than the column names of the second DataTable. So even if there are 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 are different column names of both DataTables 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.
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.
Use column index number rather than names:
destRow[0] = sourcerow[0]; // for column 0 = "Name" or "NM"
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.