I've got a datatable that I need to sort and place back into another datatable. On the face of this its easy as below:
DataTable sortme = getdata();
sortme.Select("col1 = 'something'", "sortbyme ASC").CopytoDataTable();
However, I've found as soon as I pass the DataRow array created by select() to CopytoDataTable(), the new datatable is no longer sorted by sortbyme.
How do I fix this without creating a loop to push each DataRow into a datatable? And what is causing the sorting to be lost?
There is already a question asked and solved about how to sort DataTables by row, please look at it here, it might help you:
Sorting rows in a data table
Related
This question already has answers here:
Simple way to convert datarow array to datatable
(14 answers)
Closed 1 year ago.
I have a DataGridView that im trying to empty and then put data back into but no matter what method I try to fill the dgv I only get empty rows.
I have this code but no matter what I try, dt.Rows.Add(sortestStus[row]) always results in an empty row being added.
sortedStus = Split_List(stuRows);
DataTable dt = (DataTable)dgvClassStuInfo.DataSource;
dgvClassStuInfo.DataSource = dt;
dt.Clear();
for (int row = 0; row < sortedStus.Count; row++)
{
dt.Rows.Add(sortedStus[row]);
}
sortedStus is a List<DataRow> filled by a merge sort - this works as intended and sortedStus is filled with rows that have their intended data. dt.Clear(); is working as intended and removing all rows but its the for loop that is causing issues.
I've tried dt.NewRow(); and adding each item individually but it still didn't work and I've also tried refreshing both the dgv and the datatable and that also didn't work. Anyone have any ideas?
ETA: sorry if this is a duplicate question but I can't seem to find any with this problem, only trying to actually add the empty rows.
ETA2: trying dt.Rows.Clear(); as Anonymous suggested has the same behaviour
I FIGURED OUT A SOLUTION
my question was actually a duplicate of Simple way to convert datarow array to datatable
I needed to initialise dt as a new instance of DataTable and fill it using sortedStus.CopyToDataTable() and then setting the datasource of my DataGridView to dt.
Closing question now.
I have a DataView (with sort columns, and no RowFilter) over a DataTable. When I call DataView.FindRows(SortCols[]) it returns DataRowView[].
What is the most efficient way (in terms of speed), to bind this array to a DataGridView?
My google searches returned just a couple of relevant results, and they seem to recommend iterating over the array and adding the data to a new DataTable, which could then be bound.
I expect my DataRowView[] to contain at least a few hundred items, so I would not like to go the iteration way unless nothing better is available.
Any suggestions would be appreciated. Many thanks in advance.
After a little trial and error, I ended up with this solution. I do iterate over the array, but could avoid second level iteration over columns:
DataRowView[] drvFiltered = NFM.dtvFilterRothAppMsgs.FindRows(new string[] { sSenderCompId, sTargetCompId });
//-- Current solution
DataRow dr;
foreach (DataRowView drv in drvFiltered) {
dr = dtbFilteredAppMsgs.NewRow();
dr.ItemArray = drv.Row.ItemArray;
dtbFilteredAppMsgs.Rows.Add(dr);
}
dtbFilteredAppMsgs.AcceptChanges();
It works, for now. If anybody knows of a more direct method, please do let me know. Many thanks.
This question is about finding a more efficient way for a simple problem. I have two DataTables with same structure (i.e. the Columns have same name with same Ordinals). Let them Call DataTable A and DataTable B. Assume both have 100 rows. Now I want to copy all the rows of DataTable B to DataTable A without removing rows from DataTable A. So in the end DataTable A has 200 rows. I did it as shown below.
for (int i = 0; i < B.Rows.Count - 1;i++ )
{
DataRow dr = B.Rows[i];
A.Rows.Add(dr);
}
The issue is I do not want to loop. Is there a direct way to copy it, without looping. The whole 100 rows at once. Is there a function which specifies the set of rows you want to copy.
As far as I know, there is no other way of copying multiple rows from one Datatable to another than iterating through all the rows. In fact, on MSDN there is an article telling you how to copy rows between Datatables and uses an iteration loop.
https://support.microsoft.com/en-gb/kb/305346
There are some problems with your simple approach because it doesnt handle primary key violations. Try BeginLoadData, LoadDataRow and EndLoadData. This should be more efficient. BeginLoadData and EndLoadData call only once.
If you just need a new independent DataTable instance to work with and do not need to append rows to an existing DataTable, then the DataView.ToTable() method is very convenient.
https://msdn.microsoft.com/en-us/library/a8ycds2f(v=vs.110).aspx
It creates a separate copy with the same schema and content.
DataTable objTableB = objTableA.DefaultView.ToTable();
So basically, I have a dataset which contains several datatables, from which columns are removed in my program.
At some point I want to reset my datatable so that all changes to the columns are gone.
Because I can't just create a new instance of that datatable in my dataset, I do this:
this._dsTest = new DsTest();
//Removing/Adding the first datatable
this._dsTest.Tables.Remove(this._dsTest.TestTable);
this._dsTest.Tables.Add(new DsTest.TestTableDataTable());
//Removing/Adding the second datatable
this._dsTest.Tables.Remove(this._dsTest.ReloadTable);
this._dsTest.Tables.Add(new DsTest.ReloadTableDataTable());
However, if I try to access the second datatable, I receive a NullReferenceException, telling me that the second dataTable is null.
What I don't understand is:
If I would add another table to my dataset, that third table would throw the NullReferenceException once I try to access it, and the second one would work without any problems.
Is there anything I am missing that would explain this behaviour?
Update:
This is the code I am using to access the datatables:
this._dsTest.TestTable.AddTestTableRow("Test", 1);
this._dsTest.ReloadTable.AddReloadTableRow(1, "Reload", 1); //This throws the exception
When you made any changes to your dataset / datatable you should use AcceptChanges method.
For example :
this._dsTest = new DsTest();
//Removing/Adding the first datatable
this._dsTest.Tables.Remove(this._dsTest.TestTable);
this._dsTest.AcceptChanges();
this._dsTest.Tables.Add(new DsTest.TestTableDataTable());
this._dsTest.AcceptChanges();
So, I finally found the answer to my problem.
The reason that I received the NullReferenceException when I tried to do
this._dsTest.ReloadTable.AddReloadTableRow(1, "Reload", 1)
was, that the datatable that I added to the dataset was working perfectly fine, but the public property of that datatable (this._dsTest.ReloadTable)
was not set to the datatable I had juts added.
So the solution for this is to simply do this:
this._dsTest.InitVars();
After that, accessing the DataTable works without a problem.
Thanks to anyone who tried to help me with this.
I am new to C#.net.I want a simple solution for my problem.
I have a datatable with three fields used as a PRIMARY KEY**(ITEM_CODE,WAREHOUSE_CODE,LOTNO)**.
I want to get the row index of the row that I 'm trying to match with this PRIMARY KEY from my datatable.
How can I do this?
Thanks a lot for your help and interest...
I solved my problem with this code,
int rowIndex = dt.Rows.IndexOf(dt.Select("ITEM_CODE = 'i001' AND WAREHOUSE_CODE='001' AND LOTNO='111'")[0]);
Thank you for all of your interest and answers.
Call the Find() method to get the reference of DataRow object based upon the given values and if it is (DataRow object) not null then compare its reference to each row of Rows collection of Datatable and obtain the row index.