how to get the xtragrid filtered and sorted datasource?

假装没事ソ 提交于 2019-11-28 06:30:00

问题


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, I have a Stimulsoft report that shows the content of the gridView when the user clicks on a PrintListButton, how to get the xtragrid filtered and sorted datasource, in order to attach it to the report? Thanks.


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


来源:https://stackoverflow.com/questions/11228449/how-to-get-the-xtragrid-filtered-and-sorted-datasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!