DataTable, How to conditionally delete rows

You could query the dataset and then loop the selected rows to set them as delete. var rows = dt.Select(“col1 > 5”); foreach (var row in rows) { row.Delete(); } dt.AcceptChanges(); … and you could also create some extension methods to make it easier … myTable.Delete(“col1 > 5”); public static DataTable Delete(this DataTable table, string … Read more

Find row in datatable with specific id

Make a string criteria to search for, like this: string searchExpression = “ID = 5” Then use the .Select() method of the DataTable object, like this: DataRow[] foundRows = YourDataTable.Select(searchExpression); Now you can loop through the results, like this: int numberOfCalls; bool result; foreach(DataRow dr in foundRows) { // Get value of Calls here result … Read more

How to convert DataTable to class Object?

Initialize DataTable: DataTable dt = new DataTable(); dt.Columns.Add(“id”, typeof(String)); dt.Columns.Add(“name”, typeof(String)); for (int i = 0; i < 5; i++) { string index = i.ToString(); dt.Rows.Add(new object[] { index, “name” + index }); } Query itself: IList<Class1> items = dt.AsEnumerable().Select(row => new Class1 { id = row.Field<string>(“id”), name = row.Field<string>(“name”) }).ToList();

get index of DataTable column with name

You can use DataColumn.Ordinal to get the index of the column in the DataTable. So if you need the next column as mentioned use Column.Ordinal + 1: row[row.Table.Columns[“ColumnName”].Ordinal + 1] = someOtherValue; Warning: This code returns the next column, so the one after ColumnName, as requested in the question.

Getting a count of rows in a datatable that meet certain criteria

One easy way to accomplish this is combining what was posted in the original post into a single statement: int numberOfRecords = dtFoo.Select(“IsActive=”Y””).Length; Another way to accomplish this is using Linq methods: int numberOfRecords = dtFoo.AsEnumerable().Where(x => x[“IsActive”].ToString() == “Y”).ToList().Count; Note this requires including System.Linq.

Datatable to html Table

use this function: public static string ConvertDataTableToHTML(DataTable dt) { string html = “<table>”; //add header row html += “<tr>”; for(int i=0;i<dt.Columns.Count;i++) html+=”<td>”+dt.Columns[i].ColumnName+”</td>”; html += “</tr>”; //add rows for (int i = 0; i < dt.Rows.Count; i++) { html += “<tr>”; for (int j = 0; j< dt.Columns.Count; j++) html += “<td>” + dt.Rows[i][j].ToString() + “</td>”; … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)