如何获取xtragrid过滤和排序的数据源?

时间:2012-06-27 14:25:31

标签: datasource xtragrid

我有一个绑定到bindingSource的xtraGrid控件(v12.1),最后从LINQ到实体查询(EF4.3.1)获取其数据,最终用户可以对gridView进行过滤和排序,我有一个Stimulsoft报告当用户点击PrintListButton时,它显示了gridView的内容,如何获取xtragrid过滤和排序的数据源,以便将其附加到报告中? 感谢。

1 个答案:

答案 0 :(得分:1)

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(',', ' ');
        }