Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

how to get the xtragrid filtered and sorted datasource?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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(',', ' ');
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...