Sorting rows in a data table

I’m afraid you can’t easily do an in-place sort of a DataTable like it sounds like you want to do. What you can do is create a new DataTable from a DataView that you create from your original DataTable. Apply whatever sorts and/or filters you want on the DataView and then create a new DataTable … Read more

How to show details of current row from p:dataTable in a p:dialog and update after save

The button should be an ajax button which sets the currently iterated entity in the bean, and then updates the dialog’s content, and finally shows it. The dialog should just reference that entity in the bean and update the list and table on save. It’s very important that dialog is placed outside the main form … Read more

Inner join of DataTables in C#

If you are allowed to use LINQ, take a look at the following example. It creates two DataTables with integer columns, fills them with some records, join them using LINQ query and outputs them to Console. DataTable dt1 = new DataTable(); dt1.Columns.Add(“CustID”, typeof(int)); dt1.Columns.Add(“ColX”, typeof(int)); dt1.Columns.Add(“ColY”, typeof(int)); DataTable dt2 = new DataTable(); dt2.Columns.Add(“CustID”, typeof(int)); dt2.Columns.Add(“ColZ”, … Read more

Convert IEnumerable to DataTable

Look at this one: Convert List/IEnumerable to DataTable/DataView In my code I changed it into a extension method: public static DataTable ToDataTable<T>(this List<T> items) { var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach(var prop in props) { tb.Columns.Add(prop.Name, prop.PropertyType); } foreach (var item in items) { var values = new object[props.Length]; … Read more

How do you convert a DataTable into a generic list?

If you’re using .NET 3.5, you can use DataTableExtensions.AsEnumerable (an extension method) and then if you really need a List<DataRow> instead of just IEnumerable<DataRow> you can call Enumerable.ToList: IEnumerable<DataRow> sequence = dt.AsEnumerable(); or using System.Linq; … List<DataRow> list = dt.AsEnumerable().ToList();

How to export DataTable to Excel

I would recommend ClosedXML – You can turn a DataTable into an Excel worksheet with some very readable code: XLWorkbook wb = new XLWorkbook(); DataTable dt = GetDataTableOrWhatever(); wb.Worksheets.Add(dt,”WorksheetName”); The developer is responsive and helpful. The project is actively developed, and the documentation is superb.

Convert datatable to JSON in C#

This code snippet from Convert Datatable to JSON String in C#, VB.NET might help you. It uses System.Web.Script.Serialization.JavaScriptSerializer to serialize the contents to JSON format: public string ConvertDataTabletoString() { DataTable dt = new DataTable(); using (SqlConnection con = new SqlConnection(“Data Source=SureshDasari;Initial Catalog=master;Integrated Security=true”)) { using (SqlCommand cmd = new SqlCommand(“select title=City,lat=latitude,lng=longitude,description from LocationDetails”, con)) { … Read more

Convert JSON to DataTable

There is an easier method than the other answers here, which require first deserializing into a c# class, and then turning it into a datatable. It is possible to go directly to a datatable, with JSON.NET and code like this: DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));