I create a datatable and assign it to UltraGrid datasource. The sort order from datatable is not preserved.
e.g. Datatable column sort order (col1, col2, col3). UltraGrid Column sort order (col1, col3, col2).
I want to preserve the UltraGrid column sort order to the sort order in DataTable
e.g. Datatable column sort order (col1, col2, col3).UltraGrid Column sort order (col1, col2, col3).
Please advice
---------------------------------------Here is the code i use.---------------------------
DataTable _table = new DataTable(); for (int i = 0; i < _colKeys.Length; i++) { if (!_table.Columns.Contains(_colKeys[i])) { _table.Columns.Add(_colKeys[i], typeof(string)); _table.Columns[_colKeys[i]].Caption = _colNames[i]; } } _table.Rows.Add(_table.NewRow()); // Value row _table.Rows.Add(_table.NewRow()); // NAV row _mSummaryGrid.DataSource = _table;
The grid displays the columns in the order in which the DotNet BindingManager returns them. I've never seen a case where this is different from the actual data source - assuming the data source has an actual order, which a DataTable certainly does.
So the only way this could happen is if something in your code is rearranging the columns in the grid, either in code or by loading a Layout into the grid.
I see we can manually sort the ultragrid with the below code,
UltraGrid.DisplayLayout.Bands(0).columns("ColumnName").SortIndicator = UltraWinGrid.SortIndicator.Ascending
UltraGrid.DisplayLayout.Bands(0).columns("ColumnName2").SortIndicator = UltraWinGrid.SortIndicator.Descending
This sorting is only applied on the UltraGrid, not on the dataSource used to bind the grid.
Does infragistics has any function to apply the UltraGrid's manually sorting into the dataTable as well ?
Thank you.
Hi Toji,
The grid sorts it's own data, it cannot sort the data source. The grid works with a wide variety of data sources and it does so through some common interfaces. Not ll of the data sources that can bind to the grid even support sorting, and even if they did, they interfaces the grid deals with do not. Also, there's no guarantee that the data source sort and the grid sort would match up. For example, when you sort a list with duplicate values, the order is arbitrary.
Why do you want the data source to be sorted?
If you want to get from a grid row to the data source object, you can use the ListObject property of the grid row. Similarly there are methods to find a grid row based on a list index.
Another option would be to use the grid's external sorting feature. The way it works is that you set the HeaderClickAction to one of the ExternalSort values. This allows the grid to display the sort indicator, but not do any actual sorting. It's then up to you to sort the data and refresh the grid.
I wanted to export a grid data to an excel file. I use UltraGridExcelExporter for this, which accepts a UltraGrid reference.
Our generic function which does the export accepts the UltraGrid from the screen and need to do various manipulation on the data before exporting.
The manipulations is done on the datasource(which can be dataset, datatable, dataview), after taking a copy of it, so we won't mess up the users screen datasource.
If i knew a way to copy the receiving ultragrid from the screen and do the manipulation on that copied ultragrid, then i would had done that, i didn't see a way to copy the ultraGrid, thats why i copied the datasource and doing my manipulation on the copy.
When i copy the datasource i don't get the sorting on the UltraGrid.
But i have already made a fix for it by taking the ASC and DESC sorting of all the columns in the grid and applying it on the new datasource. I did it this way:
Private Function SortedDataTable(ug As UltraGrid) As DataTable Dim sb As New System.Text.StringBuilder Dim dt = Me.Utils.GetTableFromDataSource(ug.DataSource) Dim dv = dt.DefaultView
For Each column As UltraGridColumn In ug.DisplayLayout.Bands(0).SortedColumns If {SortIndicator.Ascending, SortIndicator.Descending}.Contains(column.SortIndicator) Then If sb.Length > 0 Then sb.Append(",") End If
sb.AppendFormat("{0} {1}", column.Key, If(column.SortIndicator = SortIndicator.Ascending, "ASC", "DESC")) End If Next
dv.Sort = sb.ToString sb.Clear()
Return dv.ToTable End Function
Hi,
What kinds of modifications are you making to the data? Creating a copy of the entire data source seems like it might not be the most efficient way to go. A better way to do this might be to handle the events of the UltraGridExcelExporter like CellExported and then change the value that gets exported to the excel cell, rather than modifying the data source data directly.
If you need to copy the data source, then I assume you are creating a new grid to bind to it, is that right? You obviously don't want the on-screen grid to be showing the modified data for the export. So in that case, all you would need to do it copy the sorting from the on-screen grid into the export grid. You probably want other settings, too, like filtering, summaries, etc. You can do that very easily. There are save and load methods on the grid.DisplayLayout to allow you to save the layout to a stream or a file. But in this case where you have two in-memory grids, it's even easier - you can use the grid.DisplayLayout.CopyFrom and pass in the DisplayLayout of the on-screen grid.
One other reason for copying the datasource was we show dates in military datetime(24hrs) format and the excel don't recognize the 24hrs format, so manually i was converting the datetime columns in the datasouce to string datatype and putting the 24hrs converted time in it, so excel will display it correctly without converting according to users pc datetime settings.
Toji Mathew said:One other reason for copying the datasource was we show dates in military datetime(24hrs) format and the excel don't recognize the 24hrs format, so manually i was converting the datetime columns in the datasouce to string datatype and putting the 24hrs converted time in it, so excel will display it correctly without converting according to users pc datetime settings.
It is not correct to say that Excel doe not support 24hr dates. It definitely does.
Dates and the formats of the dates are two separate things. DateTime objects in DotNet have no inherent format and dates in Excel are stored as integers. The UltraGridExcelExporter will export the date correctly. But since the DotNet formats and Excel formats are different, the grid exporter doesn't make any attempt to convert the DotNet Format into Excel.
So all you have to do to make this work is format the dates in your exported Excel worksheet. This is typically done by handling the exporter's InitializeColumn event and setting e.ExcelFormatStr to an appropriate format string for example. Very often (but not always), you can simply use the same format string you use in DotNet:
e.ExcelFormatStr = e.FrameworkFormatStr
This is a much better solution than converting your dates into strings, since that will cause all sorts of problems with sorting, filtering, etc.