I'm getting data from my sql server in a sorted order, but the display of the grid is not showing the rows sorted.
var fieldlist = db.spFieldList(Util.GetSID());gridFieldList.DataSource = fieldlist.ToList();gridFieldList.DataBind();
I've checked that the data in DataSource is sorted. The first column in the grid is a GroupByColumn and it is sorted properly, but the rows under each group are NOT sorted.
Any idea why the sort is not "sticking"? And if no, can someone provide some code to have the grid sort itself? I don't want the end-user to be able to click on headers to change the sort.
Thanks!
Got it taken care of.
Moved the setting of the IsGroupByColumn into the DataBound event and now it looks good.
Here's the code I added:
protected void gridFieldList_DataBound(object sender, EventArgs e){//sort by the value columngridFieldList.Bands[0].SortedColumns.Clear();UltraGridColumn col = gridFieldList.Bands[0].Columns.FromKey("Value");gridFieldList.Bands[0].SortedColumns.Add(col, true);col.SortIndicator = SortIndicator.Ascending;}
so now the rows are sorted.... but I lost the grouping!
I was setting the grouping as follows:
protected void gridFieldList_InitializeLayout(object sender, Infragistics.WebUI.UltraWebGrid.LayoutEventArgs e){e.Layout.Bands[0].Columns.FromKey("Field").IsGroupByColumn = true;}
The sort you've applied on your database retrieval is lost because the group-by process itself causes the data to be sorted. Part of the process of determining what rows get grouped together is to actually sort the data.
As a solution, you can make the grid sort itself, as it seems you were already suspecting. Since you don't want the end user to be able to change the sort, this will take an additional couple of steps than normal.
First, you have to first enable sorting on the column, by setting its HeaderClickAction to SortSingle or SortMulti. The grid itself also needs to allow sorting; since the user won't be allowed to do so later, you can set the grid's DisplayLayout.AllowSortingDefault to "Yes" for server-side-only sorting. After the grid's data has been added (InitializeLayout is too early, DataBound is an alternative I've seen used, and PreRender is the latest you can do so), you have to do two things to the column: add it to its band's SortedColumns collection, and set the SortIndicator property of the column to either Ascending or Descending as desired. (You have to set the SortIndicator so the grid knows how to sort.) The following post shows some sample code:http://forums.infragistics.com/forums/p/9193/35835.aspx#35835
To keep the user from sorting on the column, you need to now disable it on the client. One way to do this is to handle the grid's client-side BeforeSortColumn event and cancel it:
function UltraWebGrid1_BeforeSortColumnHandler(gridName, columnId){ return true;}