how to get the xtragrid filtered and sorted datasource?

前端 未结 1 1765
情话喂你
情话喂你 2020-12-20 08:37

I have an xtraGrid control (v12.1) binded to a bindingSource, this last gets its data from a LINQ to entities query (EF4.3.1), the end user can filter and sort the gridView,

相关标签:
1条回答
  • 2020-12-20 08:58
    var data = GetDataView(xtraGridControl1);
    report.RegData("List", data.ToTable());
    
    
            public DataView GetDataView(GridControl gc)
            {
                DataView dv = null;
    
                if (gc.FocusedView != null && gc.FocusedView.DataSource != null)
                {
                    var view = (ColumnView)gc.FocusedView;
                    var currentList = listBindingSource.List.CopyToDataTable().DefaultView; //(DataView)
    
                    var filterExpression = GetFilterExpression(view);
                    var sortExpression = GetSortExpression(view);
    
                    var currentFilter = currentList.RowFilter;
    
                    //create a new data view 
                    dv = new DataView(currentList.Table) {Sort = sortExpression};
    
                    if (filterExpression != String.Empty)
                    {
                        if (currentFilter != String.Empty)
                        {
                            currentFilter += " AND ";
                        }
                        currentFilter += filterExpression;
                    }
                    dv.RowFilter = currentFilter;
                }
                return dv;
            }
    
            public string GetFilterExpression(ColumnView view)
            {
                var expression = String.Empty;
    
                if (view.ActiveFilter != null && view.ActiveFilterEnabled
                              && view.ActiveFilter.Expression != String.Empty)
                {
                    expression = view.ActiveFilter.Expression;
                }
                return expression;
            }
    
            public string GetSortExpression(ColumnView view)
            {
                var expression = String.Empty;
                foreach (GridColumnSortInfo info in view.SortInfo)
                {
                    expression += string.Format("[{0}]", info.Column.FieldName);
    
                    if (info.SortOrder == DevExpress.Data.ColumnSortOrder.Descending)
                        expression += " DESC";
                    else
                        expression += " ASC";
                    expression += ", ";
                }
                return expression.TrimEnd(',', ' ');
            }
    
    0 讨论(0)
提交回复
热议问题