How can I convert a DataTable into a Dynamic object?

class Program { static void Main() { var dt = new DataTable(); dt.Columns.Add(“ID”, typeof(int)); dt.Columns.Add(“Name”, typeof(string)); dt.Rows.Add(1, “x”); dt.Rows.Add(2, “y”); List<dynamic> dynamicDt = dt.ToDynamic(); Console.WriteLine(dynamicDt.First().ID); Console.WriteLine(dynamicDt.First().Name); } } public static class DataTableExtensions { public static List<dynamic> ToDynamic(this DataTable dt) { var dynamicDt = new List<dynamic>(); foreach (DataRow row in dt.Rows) { dynamic dyn = new … Read more

How to Edit a row in the datatable

First you need to find a row with id == 2 then change the name so: foreach(DataRow dr in table.Rows) // search whole table { if(dr[“Product_id”] == 2) // if id==2 { dr[“Product_name”] = “cde”; //change the name //break; break or not depending on you } } You could also try these solutions: table.Rows[1][“Product_name”] = … Read more

DataTable internal index is corrupted

I just had the same issue while importing rows, as it seems, calling DataTable.BeginLoadData before the insert fixed it for me. Edit: As it turns out, this only fixed it on one side, now adding rows throws this exception. Edit2: Suspending binding as suggested by Robert Rossney fixed the adding problem for me, too. I … Read more

How to make datatable row or cell clickable?

To activate click on cell you must use a delegated event handler – this will work on any dataTable : $(‘.dataTable’).on(‘click’, ‘tbody td’, function() { //get textContent of the TD console.log(‘TD cell textContent : ‘, this.textContent) //get the value of the TD using the API console.log(‘value by API : ‘, table.cell({ row: this.parentNode.rowIndex, column : … Read more

Mapping columns in a DataTable to a SQL table with SqlBulkCopy

You probably need some thing like public void BatchBulkCopy(DataTable dataTable, string DestinationTbl, int batchSize) { // Get the DataTable DataTable dtInsertRows = dataTable; using (SqlBulkCopy sbc = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity)) { sbc.DestinationTableName = DestinationTbl; // Number of records to be processed in one go sbc.BatchSize = batchSize; // Add your column mappings here sbc.ColumnMappings.Add(“field1″,”field3”); sbc.ColumnMappings.Add(“foo”,”bar”); … Read more

Print Contents Of A DataTable

you can try this code : foreach(DataRow dataRow in Table.Rows) { foreach(var item in dataRow.ItemArray) { Console.WriteLine(item); } } Update 1 DataTable Table = new DataTable(“TestTable”); using(SqlCommand _cmd = new SqlCommand(queryStatement, _con)) { SqlDataAdapter _dap = new SqlDataAdapter(_cmd); _con.Open(); _dap.Fill(Table); _con.Close(); } Console.WriteLine(Table.Rows.Count); foreach(DataRow dataRow in Table.Rows) { foreach(var item in dataRow.ItemArray) { Console.WriteLine(item); } … 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();

Datatable select with multiple conditions

Yes, the DataTable.Select method supports boolean operators in the same way that you would use them in a “real” SQL statement: DataRow[] results = table.Select(“A = ‘foo’ AND B = ‘bar’ AND C = ‘baz'”); See DataColumn.Expression in MSDN for the syntax supported by DataTable’s Select method.