Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/WPF/CS/PivotGridDemo.Wpf/Modules/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/WPF/CS/PivotGridDemo.Wpf/Modules/AsyncMode.xaml.cs

using System;
using System.Windows;
using DevExpress.Xpf.Core;
using DevExpress.Xpf.Grid;
using DevExpress.Xpf.PivotGrid;
using PivotGridDemo.PivotGrid.Helpers;
using System.Diagnostics;

namespace PivotGridDemo.PivotGrid {
    public partial class AsyncMode : PivotGridDemoModule {
        const string TableConnectError = "A connection error occurred. Please make sure that you have generated a data source and provided proper connection settings.";
        const string OlapConnectError = "A connection error occurred. Please make sure that you have provided proper connection settings.\nTo connect to OLAP cubes, you should have Microsoft SQL Server 2005 Analysis Services 9.0 OLE DB provider installed on your system.";
        bool isDataBaseGeneratorRunned = false;
        InitializerDataSource currentDataSource;
        double lastSplitterY = 200;

        public AsyncMode() {
            InitializeComponent();
        }
        void PivotGridDemoModule_Loaded(object sender, RoutedEventArgs e) {
            ServerParameters.LoadParameters();
            radioOlapCube.IsChecked = true;
        }
        void pivotGrid_AsyncOperationCompleted(object sender, RoutedEventArgs e) {
            EnableControls();
        }
        void pivotGrid_AsyncOperationStarting(object sender, RoutedEventArgs e) {
            DisableControls();
        }
        void btnGenerateTableData_Click(object sender, RoutedEventArgs e) {
            RunDBGenerator("Return to Demo");
        }
        void RunDBGenerator(string demoString) {
            WindowDatabaseGenerator windowDBGenerator = new WindowDatabaseGenerator(this, demoString);
            bool? cancelInitialization = windowDBGenerator.ShowDialog();
            if(cancelInitialization == false && radioTableDataSource.IsChecked.Value) {
                DisableControls();
                isDataBaseGeneratorRunned = true;
                Initialize(InitializerDataSource.TableDataSource);
            }
        }
        void radioOlapCube_Checked(object sender, RoutedEventArgs e) {
            DisableControls();
            Initialize(InitializerDataSource.OlapCube);
        }
        void radioTableDataSource_Checked(object sender, RoutedEventArgs e) {
            DisableControls();
            if(isDataBaseGeneratorRunned)
                Initialize(InitializerDataSource.TableDataSource);
            else {
                RunDBGenerator("Start Demo");
                EnableControls();
            }
        }
        void Initialize(InitializerDataSource dataSourceType) {
            currentDataSource = dataSourceType;
            AsynchronousPivotInitializer.Initialize(pivotGrid, ProcessAsyncResult, currentDataSource);
        }
        void DisableControls() {
            radioTableDataSource.IsEnabled = false;
            radioOlapCube.IsEnabled = false;
            btnGenerateTableData.IsEnabled = false;
            DispatcherHelper.DoEvents();
        }
        void EnableControls() {
            radioTableDataSource.IsEnabled = true;
            radioOlapCube.IsEnabled = true;
            btnGenerateTableData.IsEnabled = true;
        }
        void ShowErrorInfo() {
            if(currentDataSource == InitializerDataSource.OlapCube) {
                tbOlap.Visibility = System.Windows.Visibility.Visible;
                DXMessageBox.Show(OlapConnectError, "Could not connect to OLAP cube", MessageBoxButton.OK, MessageBoxImage.Error);
            } else
                DXMessageBox.Show(TableConnectError, "Could not connect to data source", MessageBoxButton.OK, MessageBoxImage.Error);
        }
        void ProcessAsyncResult(AsyncOperationResult result) {
            if(result == null) {
                if(currentDataSource == InitializerDataSource.OlapCube || isDataBaseGeneratorRunned)
                    ShowErrorInfo();
                else
                    RunDBGenerator("Start Demo");
            }
            EnableControls();
        }
        void pivotGrid_CellDoubleClick(object sender, PivotCellEventArgs e) {
            AsyncCompletedHandler showDrillDown = delegate(AsyncOperationResult result) {
                try {
                    if(result.Exception != null)
                        DXMessageBox.Show(result.Exception.Message);
                    else
                        ShowDrillDown((PivotDrillDownDataSource)result.Value);
                } catch(Exception ex) {
                    DXMessageBox.Show(ex.Message);
                }
            };
            pivotGrid.CreateDrillDownDataSourceAsync(e.ColumnIndex, e.RowIndex, showDrillDown);
        }
        void ShowDrillDown(PivotDrillDownDataSource drillDownDataSource) {
            if(drillDownDataSource.RowCount == 0) {
                DXMessageBox.Show("DrillDown operation returned no rows");
                return;
            }
            GridControl grid = new GridControl();
            grid.View = new TableView() { AllowPerPixelScrolling = true };
            grid.ItemsSource = drillDownDataSource;
            grid.AutoGeneratedColumns += grid_ColumnsPopulated;
            grid.PopulateColumns();

            FloatingContainer popupContainer = FloatingWindowContainer.ShowDialog(grid, this, new Size(520, 300),
             new FloatingContainerParameters() {
                 AllowSizing = true,
                 CloseOnEscape = true,
                 Title = String.Format("Drill Down Results: {0} Rows", drillDownDataSource.RowCount),
                 ClosedDelegate = null,
             });
            AddLogicalChild(popupContainer);
        }
        void grid_ColumnsPopulated(object sender, RoutedEventArgs e) {
            GridControl grid = (GridControl)sender;
            for(int i = 0; i < grid.Columns.Count; i++) {
                GridColumn column = grid.Columns[i];
                column.Header = GetHeaderText((string)column.FieldName);
            }
        }
        string GetHeaderText(string drilldownColumnName) {
            for(int i = 0; i < pivotGrid.Fields.Count; i++) {
                PivotGridField field = pivotGrid.Fields[i];
                string fieldDrillDownColumnName = !string.IsNullOrEmpty(pivotGrid.OlapConnectionString) ? field.OlapDrillDownColumnName : field.ExpressionFieldName;
                if(fieldDrillDownColumnName == drilldownColumnName)
                    return !string.IsNullOrEmpty(field.Caption) ? field.Caption : fieldDrillDownColumnName;
            }
            return drilldownColumnName;
        }
        void HyperlinkOlap_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e) {
            Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
            e.Handled = true;
        }

        void OnPivotGridSizeChanged(object sender, SizeChangedEventArgs e) {
            PivotGridControl pivot = sender as PivotGridControl;
            if(pivot == null || pivot.RenderSize.Height < 200 || lastSplitterY != pivot.FieldListSplitterY)
                return;
            pivot.FieldListSplitterY = Math.Round((pivot.RenderSize.Height - 90) * 0.5);
            lastSplitterY = pivot.FieldListSplitterY;
        }

        void OnPivotGridOlapException(object sender, PivotOlapExceptionEventArgs e) {
            e.Handled = true;
        }
    }
}