Reputation: 2319
I have a function which is returning the data table. this function appends one extra row that is the calculated sum. Now the requirement has come to sort the data table. I tried to sort the datatable then append the total row, but the data table is also taking the total row in consideration. How can I mitigate this behaviour?
dt.DefaultView.Sort = "totalearning DESC"; //sorting first then adding
DataRow dr = dt.NewRow();
dr.ItemArray = new object[] { "Total", dt.Compute("Sum(redeemed)", ""), dt.Compute("Sum(earning)", ""), dt.Compute("Sum(paidhousefee)", ""), dt.Compute("Sum(paidfine)", ""), dt.Compute("Sum(totalearning)", ""), dt.Compute("Sum(noofdayspresent)", ""), dt.Compute("Sum(noofdaysabsent)", ""), dt.Compute("Sum(avgdaily)", "") };
dt.Rows.InsertAt(dr, dt.Rows.Count);
Upvotes: 0
Views: 478
Reputation: 460238
I would suggest to use Linq-To-DataSet
instead which is more powerful and more readable:
dt = dt.AsEnumerable()
.OrderBy(row => row.Field<string>(0) == "Total")
.ThenByDescending(row => row.Field<int>("totalearning"))
.CopyToDataTable();
I have presumed that totalearning
is an int
-column. Correct it accordingly.
Upvotes: 2