I need to select 5 most recent rows from cached Dataview object, is there any way to do that?
I’ve tried but Indexer DataColumn is empty. :
public static DataView getLatestFourActive()
{
DataTable productDataTable = getAll().ToTable();
DataColumn ExpressionColumn = new DataColumn("Indexer",typeof(System.Int32));
ExpressionColumn.Unique = true;
ExpressionColumn.AutoIncrement = true;
ExpressionColumn.AllowDBNull = false;
ExpressionColumn.AutoIncrementSeed = 0;
ExpressionColumn.AutoIncrementStep = 1;
productDataTable.Columns.Add(ExpressionColumn);
DataView productFilteredView = productDataTable.DefaultView;
productFilteredView.RowFilter = "isActive=1 and Indexer<4";
return productFilteredView;
}
getAll() returns cached DataView
thanks
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I encountered the same sample above in this article, but the last post says the provided code doesn’t work.
However, this article has a solution that does work, so here’s the code you could use:
public static DataView getLatestFourActive() {
DataTable productDataTable = getAll().ToTable();
DataTable cloneDataTable = productDataTable.Clone();
for (int i = 0; i < 4; i++) {
cloneDataTable.ImportRow(productDataTable.Rows[i]);
}
return new DataView(cloneDataTable);
}
Method 2
getALL().Take(5);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0