How to get Paged DataTable from PagedDataSource? - datatable

I've a situation here, I'm using PagedDataSource to bind pagination to my Calender (some 3rd party control)
Here it's working fine. But problem is I need paged data; after assigning it to control. Say for e.g:
DataSet ds = DbGetEvents(DayPilotCalendar1.StartDate, DayPilotCalendar1.Days);
var pds = new PagedDataSource
{DataSource = ds.Tables[0].DefaultView, AllowPaging = true, CurrentPageIndex = _position, PageSize = 2};
Calender.DataSource=pds;
After this I need a Paged DataTable from it.
DataTable dt= /* here I need to access Paged DataTable from **paged** */
How to do this? Is It possible? Please help me.

DataView dView = (DataView)pagedDataSource.DataSource;
DataTable dTable;
dView.Sort = ordenarPor;
dTable = (DataTable)dView.Table;
pds.DataSource = dTable.DefaultView;

Related

Jqgrid: Export to excel sorting according to jqgrid table

I have added in my jqgrid to load the content in descending order. But (using OpenXML) after exporting, my data are not in descending order.
Can anyone help to get the solution?
I have added sortname:'Cust_ID' sortorder: "desc" under load jqgrid but this mentioned column Cust_ID is hidden in jqgrid. Now I would like to sort the same as my jqgrid
Below if my export to excel function.
`public string ExportReport(int cp, int noOfOrders)
{
string conndb=ConfigurationManager.AppSettings["Db"];
const string Ids ="0";
Company pList = _items.GetByCID(cp);
IQueryable<Product> List
List=product.SQLDatabase().GetListByCompandIds(cp, Convert.ToInt32(noOfOrders), conndb, Ids)
XLWorkbook workbook = new XLWorkbook(XLEventTracking.Disabled);
IXLWorksheet worksheet = workbook.Worksheets.Add("ListReport");
worksheet.Cell(1, 1).Value = "Date";
worksheet.Cell(1, 2).Value = "First Name";
worksheet.Cell(1, 3).Value = "Last Name";
worksheet.Cell(1, 4).Value = "Email";
worksheet.Cell(1, 5).Value = "Department";
worksheet.Cell(1, 6).Value = "Supervisor";
int i = 2;
foreach (EList ExpReport in List)
{
worksheet.Cell(i, 1).SetValue(ExpReport.Date);
worksheet.Cell(i, 2).SetValue(ExpReport.FirstName);
worksheet.Cell(i, 3).SetValue(ExpReport.LastName);
worksheet.Cell(i, 4).SetValue(ExpReport.Email);
worksheet.Cell(i, 5).SetValue(ExpReport.Department);
worksheet.Cell(i, 6).SetValue(ExpReport.Supervisor);
++i;
}
worksheet.Columns().AdjustToContents();
Session["Workbook"] = workbook;
string filename;
filename = "Product-Report";
return filename;
}`
Client side sorted data in jqGrid CAN NOT be accessed directly from code behind.
Alternatively, you can get the currently sorted column name from jqGrid in JavaScript, store it in a hidden field (or server control) that can be accessed from code behind, and then apply it to database query in your ExcelExport function.
For using hidden variable you may follow Persist-JavaScript-variables-and-objects-across-PostBack-in-ASPNet.aspx
In yet another approach you may get sorted column name from jqGrid in JavaScript and then use AJAX call to pass it to a WebMethod in code behind (or Web API / Script Service / Web Service such as asmx, ashx) which exports data to excel by sorting the data in required order.
For making ajax post you may refer to using-jquery-to-make-a-post-how-to-properly-supply-data-parameter For AJAX call and WebMethod usage you may follow Send-Pass-multiple-parameters-to-WebMethod-in-jQuery-AJAX-POST-in-ASPNet
If you need any reference for getting sorted column name you can refer how-to-get-the-current-sort-column-in-a-jqgrid-after-user-clicks-a-column-header

Change background colour for each var in table using AJAX

I have gone through the various answers provided on Stackflow, however cannot find the answer I need.
I need to add a different colour for each category that is called in the table. The table uses AJAX in cakePHP. The table calls correctly, but I just need to add a colour for each var.
Here is the code
function showTable() {
//var asset = document.getElementById('filter').value;
var startDate = $(".daterange").attr("data-start-date");
var endDate = $(".daterange").attr("data-end-date");
var asset = document.getElementById('assetSelect').value;
var room = document.getElementById('roomSelect').value;
var object = document.getElementById('checkobject');
var person = document.getElementById('checkPerson');
var tools = document.getElementById('checktools');
var filter = '';
You can see from above there are 5 different object, I need all the assets to be one colour, all the rooms to be another colour and so forth.
If you could help I would greatly appreciate it.
Change the style property of your object should do the job :
asset.style.background-color = '#f3f3f3';

How to Cache Multiple Versions of a Page using Razor Dynamic Query & WebCache

I decided to implement caching to improve the performance of the product pages.
Each page contains a large amount of the product's images.
I created the following code in a Razor view.
#{
var productID = UrlData[0].AsInt();
var cacheItemKey = "products";
var cacheHit = true;
var data = WebCache.Get(cacheItemKey);
var db = Database.Open("adldb");
if (data == null) {
cacheHit = false;
}
if (cacheHit == false) {
data = db.Query("SELECT * FROM Products WHERE ProductID = #0", productID).ToList();
WebCache.Set(cacheItemKey, data, 1, false);
}
}
I'm using the data with the following code:
#foreach (dynamic p in data)
{
<a href="~/Products/View/#p.ProductID"
<img src="~/Thumbnail/#p.ProductID"></a>
}
The caching code works well, but when passing the new query string parameter (changing the version of the page) the result in browser is the same for the declared cashing time.
How to make caching every version of the page?
Thanks
Oleg
A very simple approach might be to convert your key (productID) to a string and append it to the name of your cacheItemKey.
So you might consider changing the line:
var cacheItemKey = "products";
to read:
var cacheItemKey = "products" + productID.ToString();
This should produce the behavior you are looking for -- basically mimicking a VaryByParam setup.
ps. Please keep in mind I have not added any sort of defensive code, which you should do.
Hope that helps.

WPF BindingListCollectionView to ListCollectionView for DataTable as ItemsSource

I want to do custom sorting on a ListView which has a DataTable as ItemsSource:
myListView.ItemsSource = (data as DataTable);
And this are the first lines of my sorting function:
DataView view = (myListView.ItemsSource as DataTable).DefaultView;
ListCollectionView coll = (ListCollectionView)CollectionViewSource.GetDefaultView(view);
The second line throws an execption like:
Unable to cast "System.Windows.Data.BindingListCollectionView" to "System.Windows.Data.ListCollectionView"
Has anyone a solution? Thx 4 answers
It returns an ICollectionView that is not a ListCollectionView. You most likely want a view on top of a view to get the features that ListCollectionView has. And since ICollectionView implements CollectionChanged, you wouldn't want to use BindingListCollectionView.
DataView view = (myListView.ItemsSource as DataTable).DefaultView;
ListCollectionView coll = new ListCollectionView(CollectionViewSource.GetDefaultView(view));
Although an alternative would be:
DataView view = (myListView.ItemsSource as DataTable).DefaultView;
BindingListCollectionView coll = new BindingListCollectionView(view);
If you only wanted only one view.
If you are binding directly to a WPF control, it is best to bind directly to it without making a BindingListCollectionView/ListCollectionView, as DefaultView already allows sorting of the DataTable.
Binding binding = new Binding() { Source = (myListView.ItemsSource as DataTable) };
this.myListView.SetBinding(myListView.ItemsSourceProperty, binding);
DataView view = (myListView.ItemsSource as DataTable).DefaultView;
view.Sort = "Age";
Hopefully Helpful,
TamusJRoyce

Linq Query Using DataTable with Paging

I have a Linq query that I copy to a DataTable which is then used to populate a gridview. I am using a group by "key" and "count" which I evaluate in the aspx page for a master/detail gridview with a repeater.
The problem that I am encountering is that the gridview datasource and bind to the datatable is not presenting me with any additional pages that are part of the data.
My query is:
// Using Linq generate the query command from the DataTable
var query = from c in dtDataTable_GridView.AsEnumerable()
group c by c.Field<string>("CLIN") into g
select new
{
Key = g.Key,
Count = g.Count(),
Items = from i in g
select new
{
CLIN = i.Field<string>("CLIN"),
SLIN = i.Field<string>("SLIN"),
ACRN = i.Field<string>("ACRN"),
CLINType = i.Field<string>("CLINType"),
Option = i.Field<string>("Option"),
Unit = i.Field<string>("Unit")
}
};
// Use extension methods to create new DataTable from query
dtTaskOrderTable = query.CopyToDataTable();
// Set the datasource
gridview1.DataSource = dtTaskOrderTable;
// Bind to the GridView
gridview1.DataBind();
If I use the original datatable (dtDataTable_GridView) directly I have paging but once I do the Linq Query and copy it back to a new datatable (dtTaskOrderTable) I lose the paging feature.
Also how do I get a value from a column name ("Option" for instance) if it is part of "Items"?
Any help would be appreciated.
Thanks,
ChrisB
Please disregard the previous answer I will delete it
It requires ICollection interface for paging.
Neither IEnumerable nore IQuerable will not work
List<(Of <(T>)>) will work as Lists implement Icollection interface
So you need
gridview1.DataSource = dtTaskOrderTable.ToList();

Resources