How to select min and max values of a column in a datatable?
Easiar approach on datatable could be: int minLavel = Convert.ToInt32(dt.Compute(“min([AccountLevel])”, string.Empty));
Easiar approach on datatable could be: int minLavel = Convert.ToInt32(dt.Compute(“min([AccountLevel])”, string.Empty));
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
You need to loop over loadDT.Columns, like this: foreach (DataColumn column in loadDT.Columns) { Console.Write(“Item: “); Console.Write(column.ColumnName); Console.Write(” “); Console.WriteLine(row[column]); }
For the people stumbling across this question and getting a similar error message in regards to an nvarchar instead of money: The given value of type String from the data source cannot be converted to type nvarchar of the specified target column. This could be caused by a too-short column. For example, if your column … Read more
using (ExcelPackage pck = new ExcelPackage(newFile)) { ExcelWorksheet ws = pck.Workbook.Worksheets.Add(“Accounts”); ws.Cells[“A1”].LoadFromDataTable(dataTable, true); pck.Save(); } That should do the trick for you. If your fields are defined as int EPPlus will properly cast the columns into a number or float.
I assume “direction” is “ASC” or “DESC” and dt contains a column named “colName” public static DataTable resort(DataTable dt, string colName, string direction) { DataTable dtOut = null; dt.DefaultView.Sort = colName + ” ” + direction; dtOut = dt.DefaultView.ToTable(); return dtOut; } OR without creating dtOut public static DataTable resort(DataTable dt, string colName, string direction) … Read more
Without For loop: Dim newColumn As New Data.DataColumn(“Foo”, GetType(System.String)) newColumn.DefaultValue = “Your DropDownList value” table.Columns.Add(newColumn) C#: System.Data.DataColumn newColumn = new System.Data.DataColumn(“Foo”, typeof(System.String)); newColumn.DefaultValue = “Your DropDownList value”; table.Columns.Add(newColumn);
Then presumably you’ll need to lift them to the non-nullable form, using Nullable.GetUnderlyingType, and perhaps change a few null values to DbNull.Value… Change the assignment to be: row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; and when adding the columns to be: table.Columns.Add(prop.Name, Nullable.GetUnderlyingType( prop.PropertyType) ?? prop.PropertyType); And it works. (?? is the null-coalescing operator; it uses the … Read more
Try this: static void Main(string[] args) { var dt = new DataTable { Columns = { { “Lastname”,typeof(string) }, { “Firstname”,typeof(string) } } }; dt.Rows.Add(“Lennon”, “John”); dt.Rows.Add(“McCartney”, “Paul”); dt.Rows.Add(“Harrison”, “George”); dt.Rows.Add(“Starr”, “Ringo”); List<string> s = dt.AsEnumerable().Select(x => x[0].ToString()).ToList(); foreach(string e in s) Console.WriteLine(e); Console.ReadLine(); }
How about: var x = from c in dt.Columns.Cast<DataColumn>() select c.ColumnName;