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.
Related
I'm trying to do something like this to create copy of a typed datarow:
var desRow = dataTable.NewRow();
var sourceRow = dataTable.Rows[rowNum];
desRow.ItemArray = sourceRow.ItemArray.Clone() as object[];
But when I call dataTable.Clear() method it clears all fields in desRow. How to create a deep copy of a DataRow?
In your code when you create your new row you create with that row a reference to the dataTable and that is why it clears all fields.
You can work with a data table clone, that way you will have a deep copy.
DataTable dt = ...
DataTable cloneDt = dt.Clone();
DataRow row = cloneDt.Rows[number];
dt.Rows.Clear();
dt.Rows.Add(row);
This way you will have your original data table with only the selected row.
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;
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 am having an datatable which is already populated. Now i want to add few rows to that datatable ,but some of rows might already exist in the datatable.I know its unneccesary but tht is the requirement.
I tried couple of things and got "the row already exist in this table : & this row belongs to some other table" .I also tried importRow ,but i guess it avoid the duplicates by dafault.
Is there any way to do that .If the datatable has 7 rows and i want to add 3 more rows whether its already exist or not. My goal is to send 10 rows to the calling function .
Or is there any other approach altogether?
UPDATE
Using
rowsToAdd.CopyToDataTable(dsCount.Tables[2], LoadOption.PreserveChanges); works but I'm not sure it's the proper way.
You can add new rows in DataTable using code below
DataRow newRow = dataTable.NewRow();
dataTable.Rows.Add(newRow);
To check for any duplicates try
if (table.Rows.Contain(PriKeyTypeValue)) /*See if a Primary Key Value is in
the table already */
continue;
else
table.Row.Add(value1, value2, value3);
If you want to be able to insert duplicate rows but do not want to have an exception thrown set-up your primary key as a unique self-incrementing int then you can insert as many duplicates as you feel like without having to check to see if the table contains that value. Just make sure that it would suffice to have duplicates. There are plenty of examples of setting a primary key just search for it (msdn has at least one). Here is an example:
DataTable table = new DataTable();
table.Columns.Add("Column", typeof(int));
DataColumn column = table.Columns["Column"];
column.Unique = true;
column.AutoIncrement = true;
column.AutoIncrementStep = 1; //change these to whatever works for you
column.AutoIncrementSeed = 1;
table.PrimaryKey = new DataColumn[] { column };
Create a new row using the NewRow() function.
var dataTable = new DataTable();
var dataRow = dataTable.NewRow();
Then add your new row to your datatable
dataTable.Rows.Add(dataRow)
I believe you can use the NewRow() function of the DataTable if you're simply appending a row to the table.
DataTable table = new DataTable();
DataRow row = table.NewRow();
table.Rows.Add(row);
Will that not suffice?
The most straightforward method. After spending a few hours trying everything else.
DataRow dr = ds.Tables[0].NewRow();
dr["ColumnName1"] = "columnvalue"; //string
dr["ColumnName2"] = 123 //int
ds.Tables[0].Rows.Add(dr);
I want to assign a DataTable (dataTable1) to another DataTable (dataTable2) and remove some columns in the latter DataTable. For example I have the following code:
DataTable dataTable2 = dataTable1;
dataTable2.Columns.Remove("column1");
dataTable2.Columns.Remove("column2");
It turns out both DataTable (dataTable1 and dataTable2) have the columns removed. I do not understand why dataTable1 would have column1 and column2 removed as well, while I am only removing columns in dataTable2.
[EDITED - with answer]
Should use Clone() AND ImportRow() instead of a pointer assignment.
DataTable dataTable2 = dataTable1.Clone()
for (int i = 0; i < dataTable1.Rows.Count; i++)
{
dataTable2.ImportRow(dataTable1.Rows[i]);
}
This happens because dataTable2 points to the same table as dataTable1. To resolve this problem, use the DataTable's Clone method to create a new DataTable with the same structure:
DataTable dataTable2 = dataTable1.Clone();
dataTable2.Columns.Remove("column1");
dataTable2.Columns.Remove("column2");
datatable is reference type so thats why both of the tables are changing if changes are made in one. and instead of looping through data rows you could either call dataTable1.Copy() to copy data and schema to another datatable.
datatable.clone() is pass by reference. u have to give it by value.
DataTable dataTable2=dataTable1.Copy();
dataTable2.Columns.Remove("column1");
dataTable2.Columns.Remove("column2");
Now removing From datatable2 does not effect DataTable1