Deleting specific rows from DataTable
If you delete an item from a collection, that collection has been changed and you can't continue to enumerate through it.
Instead, use a For loop, such as:
for(int i = dtPerson.Rows.Count-1; i >= 0; i--)
{
DataRow dr = dtPerson.Rows[i];
if (dr["name"] == "Joe")
dr.Delete();
}
dtPerson.AcceptChanges();
Note that you are iterating in reverse to avoid skipping a row after deleting the current index.
Delete a specific row from datatable
One way is to recreate the table with the rows you want to keep:
dt = dt.AsEnumerable()
.Where(row => row.Field<string>("STOK_KODU") != "HAMMADDE_2")
.CopyToDataTable()
The other is to use DataRowCollection.Remove
:
DataRow[] rowsToRemove = dt.Select("STOK_KODU='HAMMADDE_2'");
foreach (var rowToDelete in rowsToRemove)
dt.Rows.Remove(rowToDelete);
The second approach is more efficient if you want to delete few rows and the table is large. The first approach using LINQ is more powerful since you can use any code but it can be less efficient.
Deleting row from datatable in C#
Try using Delete method:
DataRow[] drr = dt.Select("Student=' " + id + " ' ");
for (int i = 0; i < drr.Length; i++)
drr[i].Delete();
dt.AcceptChanges();
Removing specific row using datatables
Although after lots of trying ,the delete row ,ajax reload was not working as i was using PIPELINE data.So in order to refresh the ajax without losing pagination,i have to clear the pipeline cache
and then reloading the current datatables page works perfectly:
// CLEARING THE CACHE
$("#users_table").DataTable().clearPipeline();
// RELOAD CURRENT DATATABLES PAGE WITHOUT LOSING PAGINATION
$("#users_table").DataTable().ajax.reload(null, false );
And no need to delete the specific row as data is getting refreshed after delete!
UPDATE:
For deleting a specific tr with specific id
:
var id = 'row id here';
var rowid = '#row_'+id;
$("#users_table tbody").find(rowid).remove();
Remove rows from datatable matching a Liststring
Presuming the column is SkillName
List<DataRow> removeRows = dt.AsEnumerable()
.Where(r => MatchingSkills.Contains(r.Field<string>("SkillName")))
.ToList();
removeRows.ForEach(dt.Rows.Remove);
Side- note: i would use a HashSet<string>
because it would be more efficient:
var MatchingSkills = new HashSet<string>(EnteredSkills.Select(c => c.Text));
Remove rows conditionally from a data.table in R
In this scenario it is not so different than data.frame
data <- data[ menuitem != 'coffee' | amount > 0]
Delete/add row by reference it is to be implemented. You find more info in this question
Regarding speed:
1 You can benefit from keys by doing something like:
setkey(data, menuitem)
data <- data[!"coffee"]
which will be faster than data <- data[ menuitem != 'coffee']
. However to apply the same filters you asked in the question you'll need a rolling join (I've finished my lunch break I can add something later :-)).
2 Even without key data.table is much faster for relatively big table (similar speed for handful amount of rows)
dt<-data.table(id=sample(letters,1000000,T),var=rnorm(1000000))
df<-data.frame(id=sample(letters,1000000,T),var=rnorm(1000000))
library(microbenchmark)
> microbenchmark(dt[ id == "a"], df[ df$id == "a",])
Unit: milliseconds
expr min lq median uq max neval
dt[id == "a"] 24.42193 25.74296 26.00996 26.35778 27.36355 100
df[df$id == "a", ] 138.17500 146.46729 147.38646 149.06766 154.10051 100
Delete specific rows from a JQuery Datatable
You need to make sure you've got a thead
:
<div id="first">
<table id="firstTable">
<thead>
<tr>
<th>One</th>
<th>Two</th>
<th>Three</th>
<th>Yes/No</th>
</tr>
</thead>
<tbody>
<tr role="row" data-user="Yes">
<td>1 One</td>
<td>1 Two</td>
<td>1 Three</td>
<td>Yes</td>
</tr>
<tr>
<td>2 One</td>
<td>2 Two</td>
<td>2 Three</td>
<td>No</td>
</tr>
<tr>
<td>3 One</td>
<td>3 Two</td>
<td>3 Three</td>
<td>Yes</td>
</tr>
<tr>
<td>4 One</td>
<td>4 Two</td>
<td>4 Three</td>
<td>Yes</td>
</tr>
<tr>
<td>4 One</td>
<td>4 Two</td>
<td>4 Three</td>
<td>Yes</td>
</tr>
<tr>
<td>5 One</td>
<td>5 Two</td>
<td>5 Three</td>
<td>No</td>
</tr>
</tbody>
</table>
</div>
<div id="second">
<table id="secondTable">
<thead>
<tr>
<th>One</th>
<th>Two</th>
<th>Three</th>
<th>Yes/No</th>
</tr>
</thead>
<tbody>
<tr>
<td>1 One</td>
<td>1 Two</td>
<td>1 Three</td>
<td>Yes</td>
</tr>
<tr>
<td>2 One</td>
<td>2 Two</td>
<td>2 Three</td>
<td>No</td>
</tr>
<tr>
<td>3 One</td>
<td>3 Two</td>
<td>3 Three</td>
<td>Yes</td>
</tr>
<tr>
<td>4 One</td>
<td>4 Two</td>
<td>4 Three</td>
<td>Yes</td>
</tr>
<tr>
<td>4 One</td>
<td>4 Two</td>
<td>4 Three</td>
<td>Yes</td>
</tr>
<tr>
<td>5 One</td>
<td>5 Two</td>
<td>5 Three</td>
<td>No</td>
</tr>
</tbody>
</table>
</div>
<div id="third">
<table id="thirdTable">
<thead>
<tr>
<th>One</th>
<th>Two</th>
<th>Three</th>
<th>Yes/No</th>
</tr>
</thead>
<tbody>
<tr>
<td>1 One</td>
<td>1 Two</td>
<td>1 Three</td>
<td>Yes</td>
</tr>
<tr>
<td>2 One</td>
<td>2 Two</td>
<td>2 Three</td>
<td>No</td>
</tr>
<tr>
<td>3 One</td>
<td>3 Two</td>
<td>3 Three</td>
<td>Yes</td>
</tr>
<tr>
<td>4 One</td>
<td>4 Two</td>
<td>4 Three</td>
<td>Yes</td>
</tr>
<tr>
<td>4 One</td>
<td>4 Two</td>
<td>4 Three</td>
<td>Yes</td>
</tr>
<tr>
<td>5 One</td>
<td>5 Two</td>
<td>5 Three</td>
<td>No</td>
</tr>
</tbody>
</table>
</div>
Which will stop DataTables complaining, then your DataTables can be created like this:
$(function() {
var firstTable = $('#firstTable').DataTable();
var secondTable = $('#secondTable').DataTable({
"initComplete": function(settings) {
var api = this.api();
api.rows().every(function(rowIdx, tableLoop, rowLoop) {
var data = this.data();
if (data && data[3] !== "Yes") {
api.rows(rowIdx).nodes().to$().addClass('remove');
}
});
api.rows('.remove').remove().draw();
}
});
var thirdTable = $('#thirdTable').DataTable({
"initComplete": function(settings) {
var api = this.api();
api.rows().every(function(rowIdx, tableLoop, rowLoop) {
var data = this.data();
if (data && data[3] !== "No") {
api.rows(rowIdx).nodes().to$().addClass('remove');
}
});
api.rows('.remove').remove().draw();
}
});
});
Working JSFiddle here. Hope that helps (there are probably better ways of doing it TBH - perhaps someone else will chip in as this is quite hackie - it always helps to have something to look at... JSFiddle is your friend).
Remove rows from a DataTable
You can try this...
var checkValues = new string[] { "Some value1", "Some value3" };
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
dt.Columns.Add("Column3");
dt.Rows.Add(new string[] {"Some value1", "Some value2", "Some value3" });
dt.AsEnumerable().ToList().ForEach(x =>
{
if (checkValues.Contains(x["Column1"]))
{
dt1.ImportRow(x);
dt.Rows.Remove(x);
}
});
Deleted selected rows from JQuery Datatables
I think you can find your answer over here
- Basically, you have to change your line
selected = table.rows('.selected').data().toArray();
totable.rows( '.selected' ).remove().draw();
- And remove the additional code.
- You also don't have to worry about looping through the list because
table.rows('.selected')
gets all the rows with the class selected and thenremove()
deletes them all for you.
Edit: If the dataSet is not updated automatically, then I think this might answer your second query
$("#delete").on("click", function (e) {
let newDataSet = [];
table.rows( '.selected' ).remove().draw();
table.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
let row = this;
newDataSet.push(row.data());
});
dataSet = newDataSet;
});
Related Topics
How to "Flatten" or "Index" 3D-Array in 1D Array
Retrieve Credentials from Windows Credentials Store Using C#
Determine What Control the Contextmenustrip Was Used On
How to Hide a Column (Gridview) But Still Access Its Value
How to Use Reflection to Call Method by Name
What Is the Best Scripting Language to Embed in a C# Desktop Application
What Represents a Double in SQL Server
Setting/Getting the Class Properties by String Name
Wpf Binding a Listbox to an Enum, Displaying the Description Attribute
Select Parsed Int, If String Was Parseable to Int
Where's the Datetime 'Z' Format Specifier
How to Post a List of Items in MVC