当前位置:网站首页>C # to obtain the filtered or sorted data of the GridView table in devaexpress

C # to obtain the filtered or sorted data of the GridView table in devaexpress

2022-07-05 11:09:00 Milk coffee 13

One 、 Requirements describe

    At present, our business needs to obtain the right DevExpress in GridView Data content after filtering or sorting the table .

Two 、 Demand analysis

    Direct access to GirdView The data content of the table is the first bound content , It will not change after filtering or sorting with the table control ; We can directly traverse the contents of the table to fill in the new DataTable Back in .

3、 ... and 、 Implementation method

 #region    Get the filtered or sorted data of the table 
        /// <summary>
        ///  obtain GridView Filtered or sorted dataset 
        /// </summary>
        /// <typeparam name="T"> Generic class object </typeparam>
        /// <param name="gridView">gridView Components </param>
        /// <returns></returns>
        public static IEnumerable<T> GetFilterOrSortDatasOfGridView<T>(DevExpress.XtraGrid.Views.Grid.GridView gridView) where T : class
        {
            var list = new List<T>();
            for (int i = 0; i < gridView.RowCount; i++)
            {
                if (gridView.IsGroupRow(i))
                    continue;
                var entity = gridView.GetRow(i) as T;
                if (entity == null)
                    continue;
                list.Add(entity);
            }
            return list;
        }

        /// <summary>
        ///  obtain GridView Filtered or sorted dataset 
        /// </summary>
        /// <param name="gridView">gridView Components </param>
        /// <returns></returns>
        public static DataTable GetFilterOrSortDatasOfGridView(DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            DataTable _dt = gridView.GridControl.DataSource as DataTable;
            if (_dt == null) return null;

            DataTable dt = _dt.Clone();
            for (int i = 0; i < gridView.RowCount; i++)
            {
                if (gridView.IsGroupRow(i))
                    continue;
                var dr = gridView.GetDataRow(i);
                if (dr == null)
                    continue;
                dt.Rows.Add(dr.ItemArray);
            }
            return dt;
        }

        #endregion 

原网站

版权声明
本文为[Milk coffee 13]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051015004203.html