i have a DataTable with some rows, one of it is a ID (a string) and a System.DateTime. the other rows are not important now.
now i want to delete all the rows with the same ID that have an older DateTime as the newest.
is this possible?
here an example
after this i only want:
For adding delete control you can either add a delete button with each record or add checkboxes and a single delete button. In this tutorial, I show how you add multiple checkboxes and delete selected records on a single click in DataTables using jQuery AJAX.
use SetModified() method to update data table value.
Try next
public void DeleteOldById(DataTable table, int id)
{
var rows = table.Rows.Cast<DataRow>().Where(x => (int)x["ID"] == id);
DateTime specifyDate = rows.Max(x => (DateTime)x["Date"])
rows.Where(x =>(DateTime)x["Date"] < specifyDate).ToList().
ForEach(x => x.Delete());
}
public void DeleteAllOldRows(DataTable table)
{
var ids = table.Rows.Cast<DataRow>().Select(x => (int)x["ID"]).Distinct();
foreach(int id in ids)
DeleteOldById(table,id);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With