Ejemplo n.º 1
0
        private void Work(string dataColumnName, bool ignoreNull)
        {
            if (string.IsNullOrEmpty(dataColumnName))
            {
                return;
            }
            if (dataColumnName.Equals(m_LastDataColumnName, StringComparison.Ordinal) && m_LastIgnoreNull == ignoreNull)
            {
                return;
            }

            m_LastDataColumnName = dataColumnName;
            m_LastIgnoreNull     = ignoreNull;

            this.SafeInvoke(
                () =>
            {
                detailControl.Visible = false;
                detailControl.SuspendLayout();
            });
            Extensions.ProcessUIElements();
            try
            {
                this.SafeBeginInvoke(() => Text = $@"Unique Values Display - {dataColumnName} ");

                var dataColumnID   = m_DataTable.Columns[dataColumnName];
                var dictIDToRow    = new Dictionary <string, int>(StringComparer.OrdinalIgnoreCase);
                var intervalAction = new IntervalAction();

                using (var display = new FormProcessDisplay(
                           $"Processing {dataColumnName}",
                           false,
                           m_CancellationTokenSource.Token))
                {
                    display.Maximum = m_DataRow.Length;
                    display.Show(this);

                    for (var rowIndex = 0; rowIndex < m_DataRow.Length; rowIndex++)
                    {
                        if (display.CancellationToken.IsCancellationRequested)
                        {
                            return;
                        }
                        intervalAction.Invoke(row => { display.SetProcess("Getting Unique values", row, true); }, rowIndex);
                        var id = m_DataRow[rowIndex][dataColumnID.Ordinal].ToString().Trim();
                        if (ignoreNull && string.IsNullOrEmpty(id))
                        {
                            continue;
                        }
                        if (!dictIDToRow.ContainsKey(id))
                        {
                            dictIDToRow.Add(id, rowIndex);
                        }
                    }

                    this.SafeInvoke(
                        () => Text = $@"Unique Values Display - {dataColumnName} - Rows {dictIDToRow.Count}/{m_DataRow.Length}");

                    m_DataTable.BeginLoadData();
                    m_DataTable.Clear();
                    display.Maximum = dictIDToRow.Count;

                    var counter = 0;
                    foreach (var rowIndex in dictIDToRow.Values)
                    {
                        if (display.CancellationToken.IsCancellationRequested)
                        {
                            return;
                        }
                        counter++;
                        if (counter % 100 == 0)
                        {
                            intervalAction.Invoke(c => { display.SetProcess("Importing Rows to Grid", c, true); }, counter);
                        }
                        m_DataTable.ImportRow(m_DataRow[rowIndex]);
                    }

                    m_DataTable.EndLoadData();
                    detailControl.CancellationToken = m_CancellationTokenSource.Token;
                    display.Maximum = 0;
                    display.SetProcess("Sorting");
                    detailControl.SafeInvoke(
                        () =>
                    {
                        try
                        {
                            foreach (DataGridViewColumn col in detailControl.FilteredDataGridView.Columns)
                            {
                                if (col.DataPropertyName == dataColumnName)
                                {
                                    detailControl.FilteredDataGridView.Sort(col, ListSortDirection.Ascending);
                                    break;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.Warning(ex, "Processing Unique Sorting {exception}", ex.InnerExceptionMessages());
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                this.ShowError(ex);
            }
            finally
            {
                this.SafeInvoke(
                    () =>
                {
                    detailControl.Visible = true;
                    detailControl.ResumeLayout(true);
                });
            }
        }