So, I have two DataTables. I can easly ADD rows from one to another with this:
table1.Rows.Add(table2.Rows[0].ItemArray);
But I want to insert the row in particular place, not on the bottom of the table1. Tried to use the table1.Rows.InsertAt, but the problem with it is that you need the DataRow class (which I can't figure out how to get), and also you can't do table.Rows.InsertAt(table1.Rows[0], idex); because it says that the row belongs to another table.
(table2 is source, table1 - target)
You can clone the source row's items and then create a DataRow using them:
var sourceItems = (object[])(table2.Rows[0].ItemArray.Clone());
DataRow targetRow = table1.NewRow();
targetRow.ItemArray = sourceItems;
table1.Rows.InsertAt(targetRow, index);
(note that it's not clear in your question which datatable is the source and which is the target, I assumed table2 is the source and table1 the target)
Related
I have a problem.Let's explain this:
I create a DataTable 'DataT' variable and added row to this variable.SameTime i have datagridview. After i assingmented 'DataT' to DataSource property of DataGridView.
I get 'DataT' Rows.Count property.It's value 10.But this value decrease from 10 to 5 when i removed 5 Rows of DataGridView.
So why decrease my value i just remove Rows of DataGridView but i did not remove rows of 'DataT' although rows count of 'DataT' decrease from 10 to 5.
I have not solve this problem.
EDIT:
Yes i bound datatable.But same problem there is other state.I explain with a example:
DataTable Table1 = new DataTable();
DataTable Table2 = new DataTable();
DataRow Rows;
private void Form1_Load(.......)
{
Tablo1.Columns.Add("Column1");
Tablo1.Columns.Add("Column2");
Rows = Table1.NewRow();
Rows[0] = "Hello";
Rows[1] = "Word";
Table1.Rows.Add(Rows);
Rows = Table1.NewRow();
Rows[0] = "Hello";
Rows[1] = "Word";
Table1.Rows.Add(Rows);
Table2 = Table1;
datagridview1.DataSource = Table1;
//This datagridview1 has 2 Rows and Table1 has 2 Rows.
datagridview1.Rows.RemoveAt(0);
//I am Removing one Row of datagridview1.Not from Table1.
//But Automatic removing Rows from Table1.
//Result=datagridview1 has 1 Row and Table1 has 1 Row.Why do remove rows from Table1?
//Even Rows Remove from Table2 when i remove rows from datagridview1.
}
As Henk Holterman pointed out in comment there are two reasons making Table2 to change
since Table1 is used as DataSource for grid any changes in the grid will be reflected in it. This is expected behavior of GridView.
GridView has its own Rows collection that is updated when one sets DataSource. Any changes to Rows collection (like removing item) are applied to DataTable set as data source. As result datagridview1.Rows.RemoveAt(0); actually indirectly removes row from the object referenced by Table1.
Both of your variables (Table1 and Table2) point to the same object - so whenever anything happens to the DataTable (i.e. row removed by GridView) the change is visible when you check either of variables.
Following code performs "shallow copy" when you seem to expect complete clone similar to behavior of int:
Table2 = Table1;
After that assignment both Table1 and Table2 refer to DataTable created on DataTable Table1 = new DataTable(); line.
See more What is the difference between a deep copy and a shallow copy?
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 have datatable1 which has 11 columns and 100,000 rows.. I would like to do a check to see if the text in column one starts with "one" and if it does, add that row into the second datatable. I have done the below but yet still it does not work.. I get the error that the row belong to another table
foreach (DataRow r in queryDataTable.Rows)
{
if (r[0].ToString().StartsWith(queryString))
{
dt.ImportRow(r);
}
}
You cannot directly import the row of one table into another datatable. You need to create a new row and then copy the row.
Try this -
dt.Rows.Add(r.ItemArray)
Instead of your for loop, you may use LINQ to select those rows which StartsWith queryString and then you can use CopytoDataTable method to create a new table for the selected rows.
var NewTable = queryDataTable.AsEnumerable()
.Where(r => r.Field<string>(0).StartsWith(queryString))
.CopyToDataTable();
Remember to include using System.Linq; at the top.
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.