internal void OnDataGridViewElementStateChanging(DataGridViewElement element, int index, DataGridViewElementStates elementState)
        {
            DataGridViewColumn dataGridViewColumn = element as DataGridViewColumn;
            if (dataGridViewColumn != null)
            {
                // column.Frozen | .Visible | .ReadOnly changing
                switch (elementState)
                {
                    case DataGridViewElementStates.Frozen:
                    case DataGridViewElementStates.Visible:
                        if (elementState == DataGridViewElementStates.Visible)
                        {
                            if (!dataGridViewColumn.Visible &&
                                dataGridViewColumn.InheritedAutoSizeMode == DataGridViewAutoSizeColumnMode.ColumnHeader &&
                                !this.ColumnHeadersVisible)
                            {
                                throw new InvalidOperationException(SR.GetString(SR.DataGridView_CannotMakeAutoSizedColumnVisible));
                            }
                            else if (!dataGridViewColumn.Visible &&
                                dataGridViewColumn.Frozen &&
                                dataGridViewColumn.InheritedAutoSizeMode == DataGridViewAutoSizeColumnMode.Fill)
                            {
                                // alternative: throw new InvalidOperationException(SR.GetString(SR.DataGridView_CannotMakeAutoFillColumnVisible));
                                //              DataGridView_CannotMakeAutoFillColumnVisible=The column cannot be made visible because its autosizing mode is Fill and it is frozen.
                                // Removing the Fill auto size mode when frozen column becomes visible (instead of throwing an exception)
                                dataGridViewColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                            }
                            else if (dataGridViewColumn.Visible && this.ptCurrentCell.X == dataGridViewColumn.Index)
                            {
                                // Column of the current cell is made invisible. Trying to reset the current cell. May throw an exception.
                                ResetCurrentCell();
                                // [....]: Should the current cell be set to some cell after the operation?
                            }
                        }
                        if (elementState == DataGridViewElementStates.Frozen &&
                            !dataGridViewColumn.Frozen &&
                            dataGridViewColumn.Visible &&
                            dataGridViewColumn.InheritedAutoSizeMode == DataGridViewAutoSizeColumnMode.Fill)
                        {
                            // Removing the Fill auto size mode when visible column becomes frozen (instead of throwing an exception)
                            dataGridViewColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                        }
                        CorrectColumnFrozenStates(dataGridViewColumn, elementState == DataGridViewElementStates.Frozen);
                        break;

                    case DataGridViewElementStates.ReadOnly:
                        if (this.ptCurrentCell.X == dataGridViewColumn.Index &&
                            this.IsCurrentCellInEditMode &&
                            !dataGridViewColumn.ReadOnly &&
                            !this.dataGridViewOper[DATAGRIDVIEWOPER_inReadOnlyChange])
                        {
                            Debug.Assert(!this.ReadOnly);
                            // Column becomes read-only. Exit editing mode.
                            if (!EndEdit(DataGridViewDataErrorContexts.Parsing | DataGridViewDataErrorContexts.Commit,
                                        DataGridViewValidateCellInternal.Always /*validateCell*/,
                                        false /*fireCellLeave*/,
                                        false /*fireCellEnter*/,
                                        false /*fireRowLeave*/,
                                        false /*fireRowEnter*/,
                                        false /*fireLeave*/,
                                        true /*keepFocus*/,
                                        false /*resetCurrentCell*/,
                                        false /*resetAnchorCell*/))
                            {
                                throw new InvalidOperationException(SR.GetString(SR.DataGridView_CommitFailedCannotCompleteOperation));
                            }
                        }
                        break;

                    default:
                        Debug.Fail("Unexpected DataGridViewElementStates param in DataGridView.OnDataGridViewElementStateChanging");
                        break;
                }
            }
            else
            {
                DataGridViewRow dataGridViewRow = element as DataGridViewRow;
                if (dataGridViewRow != null)
                {
                    // row.Frozen | .Visible | .ReadOnly changing
                    int rowIndex = ((dataGridViewRow.Index > -1) ? dataGridViewRow.Index : index);
                    switch (elementState)
                    {
                        case DataGridViewElementStates.Frozen:
                        case DataGridViewElementStates.Visible:
                            if (elementState == DataGridViewElementStates.Visible && this.ptCurrentCell.Y == rowIndex)
                            {
                                Debug.Assert((this.Rows.GetRowState(rowIndex) & DataGridViewElementStates.Visible) != 0);
                                // Row of the current cell is made invisible. 
                                if (this.DataSource != null)
                                {
                                    Debug.Assert(this.dataConnection != null);
                                    Debug.Assert(this.dataConnection.CurrencyManager != null);
                                    Debug.Assert(this.dataConnection.CurrencyManager.Position == this.ptCurrentCell.Y);
                                    // the row associated with the currency manager's position cannot be made invisble.
                                    throw new InvalidOperationException(SR.GetString(SR.DataGridView_CurrencyManagerRowCannotBeInvisible));
                                }
                                // Trying to reset the current cell. May throw an exception.
                                ResetCurrentCell();
                                // [....]: Should the current cell be set to some cell after the operation?
                            }
                            CorrectRowFrozenStates(dataGridViewRow, rowIndex, elementState == DataGridViewElementStates.Frozen);
                            break;

                        case DataGridViewElementStates.ReadOnly:
                            if (this.ptCurrentCell.Y == rowIndex &&
                                (this.Rows.GetRowState(rowIndex) & DataGridViewElementStates.ReadOnly) == 0 &&
                                !this.ReadOnly &&
                                this.IsCurrentCellInEditMode &&
                                !this.dataGridViewOper[DATAGRIDVIEWOPER_inReadOnlyChange])
                            {
                                // Row becomes read-only. Exit editing mode.
                                if (!EndEdit(DataGridViewDataErrorContexts.Parsing | DataGridViewDataErrorContexts.Commit,
                                            DataGridViewValidateCellInternal.Always /*validateCell*/,
                                            false /*fireCellLeave*/,
                                            false /*fireCellEnter*/,
                                            false /*fireRowLeave*/,
                                            false /*fireRowEnter*/,
                                            false /*fireLeave*/,
                                            true /*keepFocus*/,
                                            false /*resetCurrentCell*/,
                                            false /*resetAnchorCell*/))
                                {
                                    throw new InvalidOperationException(SR.GetString(SR.DataGridView_CommitFailedCannotCompleteOperation));
                                }
                            }
                            break;

                        default:
                            Debug.Fail("Unexpected DataGridViewElementStates param in DataGridView.OnDataGridViewElementStateChanging");
                            break;
                    }
                }
                else
                {
                    DataGridViewCell dataGridViewCell = element as DataGridViewCell;
                    if (dataGridViewCell != null)
                    {
                        // cell.ReadOnly changing
                        switch (elementState)
                        {
                            case DataGridViewElementStates.ReadOnly:
                                if (this.ptCurrentCell.X == dataGridViewCell.ColumnIndex &&
                                    this.ptCurrentCell.Y == dataGridViewCell.RowIndex &&
                                    this.IsCurrentCellInEditMode &&
                                    !dataGridViewCell.ReadOnly &&
                                    !this.dataGridViewOper[DATAGRIDVIEWOPER_inReadOnlyChange])
                                {
                                    Debug.Assert(!this.Columns[dataGridViewCell.ColumnIndex].ReadOnly);
                                    Debug.Assert(!this.Rows[dataGridViewCell.RowIndex].ReadOnly);
                                    Debug.Assert(!this.ReadOnly);
                                    // Current cell becomes read-only. Exit editing mode.
                                    if (!EndEdit(DataGridViewDataErrorContexts.Parsing | DataGridViewDataErrorContexts.Commit,
                                                DataGridViewValidateCellInternal.Always /*validateCell*/,
                                                false /*fireCellLeave*/,
                                                false /*fireCellEnter*/,
                                                false /*fireRowLeave*/,
                                                false /*fireRowEnter*/,
                                                false /*fireLeave*/,
                                                true /*keepFocus*/,
                                                false /*resetCurrentCell*/,
                                                false /*resetAnchorCell*/))
                                    {
                                        throw new InvalidOperationException(SR.GetString(SR.DataGridView_CommitFailedCannotCompleteOperation));
                                    }
                                }
                                break;

                            default:
                                Debug.Fail("Unexpected DataGridViewElementStates param in DataGridView.OnDataGridViewElementStateChanging");
                                break;
                        }
                    }
                    else
                    {
                        Debug.Fail("Unexpected DataGridViewElement type in DataGridView.OnDataGridViewElementStateChanging");
                    }
                }
            }
        }
 internal DataGridViewElement(DataGridViewElement dgveTemplate)
 {
     // Selected and Displayed states are not inherited
     _state = dgveTemplate.State & (DataGridViewElementStates.Frozen | DataGridViewElementStates.ReadOnly | DataGridViewElementStates.Resizable | DataGridViewElementStates.ResizableSet | DataGridViewElementStates.Visible);
 }
Exemple #3
0
 internal DataGridViewElement(DataGridViewElement dgveTemplate)
 {
     this.state = dgveTemplate.State & (DataGridViewElementStates.Visible | DataGridViewElementStates.ResizableSet | DataGridViewElementStates.Resizable | DataGridViewElementStates.ReadOnly | DataGridViewElementStates.Frozen);
 }
        internal void OnDataGridViewElementStateChanged(DataGridViewElement element, int index, DataGridViewElementStates elementState)
        {
            DataGridViewColumn dataGridViewColumn = element as DataGridViewColumn;
            if (dataGridViewColumn != null)
            {
                DataGridViewColumnStateChangedEventArgs dgvcsce = new DataGridViewColumnStateChangedEventArgs(dataGridViewColumn, elementState);

                OnColumnStateChanged(dgvcsce);
            }
            else
            {
                DataGridViewRow dataGridViewRow = element as DataGridViewRow;
                if (dataGridViewRow != null)
                {
                    DataGridViewRowStateChangedEventHandler eh = this.Events[EVENT_DATAGRIDVIEWROWSTATECHANGED] as DataGridViewRowStateChangedEventHandler;

                    if (eh != null && dataGridViewRow.DataGridView != null && dataGridViewRow.Index == -1)
                    {
                        dataGridViewRow = this.Rows[index];
                    }

                    DataGridViewRowStateChangedEventArgs dgvrsce = new DataGridViewRowStateChangedEventArgs(dataGridViewRow, elementState);

                    OnRowStateChanged(dataGridViewRow.Index == -1 ? index : dataGridViewRow.Index, dgvrsce);
                }
                else
                {
                    DataGridViewCell dataGridViewCell = element as DataGridViewCell;
                    if (dataGridViewCell != null)
                    {
                        DataGridViewCellStateChangedEventArgs dgvcsce = new DataGridViewCellStateChangedEventArgs(dataGridViewCell, elementState);

                        OnCellStateChanged(dgvcsce);
                    }
                }
            }

            if ((elementState & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected)
            {
                if (this.noSelectionChangeCount > 0)
                {
                    this.dataGridViewState2[DATAGRIDVIEWSTATE2_raiseSelectionChanged] = true;
                }
                else
                {
                    OnSelectionChanged(EventArgs.Empty);
                }
            }
        }
 internal DataGridViewElement(DataGridViewElement dgveTemplate)
 {
     this.state = dgveTemplate.State & (DataGridViewElementStates.Visible | DataGridViewElementStates.ResizableSet | DataGridViewElementStates.Resizable | DataGridViewElementStates.ReadOnly | DataGridViewElementStates.Frozen);
 }
		public void TestDefaultValues () {
			DataGridViewElement element = new DataGridViewElement();
			Assert.AreEqual (null, element.DataGridView, "#A1");
		}
 internal DataGridViewElement(DataGridViewElement dgveTemplate)
 {
     // Selected and Displayed states are not inherited
     this.state = dgveTemplate.State & (DataGridViewElementStates.Frozen | DataGridViewElementStates.ReadOnly | DataGridViewElementStates.Resizable | DataGridViewElementStates.ResizableSet | DataGridViewElementStates.Visible);
 }
        internal void OnDataGridViewElementStateChanging(DataGridViewElement element, int index, DataGridViewElementStates elementState)
        {
            DataGridViewColumn dataGridViewColumn = element as DataGridViewColumn;
            if (dataGridViewColumn != null)
            {
                switch (elementState)
                {
                    case DataGridViewElementStates.Frozen:
                    case DataGridViewElementStates.Visible:
                        if (elementState == DataGridViewElementStates.Visible)
                        {
                            if ((!dataGridViewColumn.Visible && (dataGridViewColumn.InheritedAutoSizeMode == DataGridViewAutoSizeColumnMode.ColumnHeader)) && !this.ColumnHeadersVisible)
                            {
                                throw new InvalidOperationException(System.Windows.Forms.SR.GetString("DataGridView_CannotMakeAutoSizedColumnVisible"));
                            }
                            if ((!dataGridViewColumn.Visible && dataGridViewColumn.Frozen) && (dataGridViewColumn.InheritedAutoSizeMode == DataGridViewAutoSizeColumnMode.Fill))
                            {
                                dataGridViewColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                            }
                            else if (dataGridViewColumn.Visible && (this.ptCurrentCell.X == dataGridViewColumn.Index))
                            {
                                this.ResetCurrentCell();
                            }
                        }
                        if (((elementState == DataGridViewElementStates.Frozen) && !dataGridViewColumn.Frozen) && (dataGridViewColumn.Visible && (dataGridViewColumn.InheritedAutoSizeMode == DataGridViewAutoSizeColumnMode.Fill)))
                        {
                            dataGridViewColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                        }
                        this.CorrectColumnFrozenStates(dataGridViewColumn, elementState == DataGridViewElementStates.Frozen);
                        return;

                    case (DataGridViewElementStates.Frozen | DataGridViewElementStates.Displayed):
                        return;

                    case DataGridViewElementStates.ReadOnly:
                        if ((((this.ptCurrentCell.X == dataGridViewColumn.Index) && this.IsCurrentCellInEditMode) && (!dataGridViewColumn.ReadOnly && !this.dataGridViewOper[0x4000])) && !this.EndEdit(DataGridViewDataErrorContexts.Commit | DataGridViewDataErrorContexts.Parsing, DataGridViewValidateCellInternal.Always, false, false, false, false, false, true, false, false))
                        {
                            throw new InvalidOperationException(System.Windows.Forms.SR.GetString("DataGridView_CommitFailedCannotCompleteOperation"));
                        }
                        return;
                }
            }
            else
            {
                DataGridViewRow dataGridViewRow = element as DataGridViewRow;
                if (dataGridViewRow != null)
                {
                    int rowIndex = (dataGridViewRow.Index > -1) ? dataGridViewRow.Index : index;
                    switch (elementState)
                    {
                        case DataGridViewElementStates.Frozen:
                        case DataGridViewElementStates.Visible:
                            if ((elementState == DataGridViewElementStates.Visible) && (this.ptCurrentCell.Y == rowIndex))
                            {
                                if (this.DataSource != null)
                                {
                                    throw new InvalidOperationException(System.Windows.Forms.SR.GetString("DataGridView_CurrencyManagerRowCannotBeInvisible"));
                                }
                                this.ResetCurrentCell();
                            }
                            this.CorrectRowFrozenStates(dataGridViewRow, rowIndex, elementState == DataGridViewElementStates.Frozen);
                            return;

                        case (DataGridViewElementStates.Frozen | DataGridViewElementStates.Displayed):
                            return;

                        case DataGridViewElementStates.ReadOnly:
                            if ((((this.ptCurrentCell.Y == rowIndex) && ((this.Rows.GetRowState(rowIndex) & DataGridViewElementStates.ReadOnly) == DataGridViewElementStates.None)) && (!this.ReadOnly && this.IsCurrentCellInEditMode)) && (!this.dataGridViewOper[0x4000] && !this.EndEdit(DataGridViewDataErrorContexts.Commit | DataGridViewDataErrorContexts.Parsing, DataGridViewValidateCellInternal.Always, false, false, false, false, false, true, false, false)))
                            {
                                throw new InvalidOperationException(System.Windows.Forms.SR.GetString("DataGridView_CommitFailedCannotCompleteOperation"));
                            }
                            return;
                    }
                }
                else
                {
                    DataGridViewCell cell = element as DataGridViewCell;
                    if (cell != null)
                    {
                        DataGridViewElementStates states3 = elementState;
                        if ((states3 == DataGridViewElementStates.ReadOnly) && ((((this.ptCurrentCell.X == cell.ColumnIndex) && (this.ptCurrentCell.Y == cell.RowIndex)) && (this.IsCurrentCellInEditMode && !cell.ReadOnly)) && (!this.dataGridViewOper[0x4000] && !this.EndEdit(DataGridViewDataErrorContexts.Commit | DataGridViewDataErrorContexts.Parsing, DataGridViewValidateCellInternal.Always, false, false, false, false, false, true, false, false))))
                        {
                            throw new InvalidOperationException(System.Windows.Forms.SR.GetString("DataGridView_CommitFailedCannotCompleteOperation"));
                        }
                    }
                }
            }
        }
 internal void OnDataGridViewElementStateChanged(DataGridViewElement element, int index, DataGridViewElementStates elementState)
 {
     DataGridViewColumn dataGridViewColumn = element as DataGridViewColumn;
     if (dataGridViewColumn != null)
     {
         DataGridViewColumnStateChangedEventArgs e = new DataGridViewColumnStateChangedEventArgs(dataGridViewColumn, elementState);
         this.OnColumnStateChanged(e);
     }
     else
     {
         DataGridViewRow dataGridViewRow = element as DataGridViewRow;
         if (dataGridViewRow != null)
         {
             if (((base.Events[EVENT_DATAGRIDVIEWROWSTATECHANGED] is DataGridViewRowStateChangedEventHandler) && (dataGridViewRow.DataGridView != null)) && (dataGridViewRow.Index == -1))
             {
                 dataGridViewRow = this.Rows[index];
             }
             DataGridViewRowStateChangedEventArgs args2 = new DataGridViewRowStateChangedEventArgs(dataGridViewRow, elementState);
             this.OnRowStateChanged((dataGridViewRow.Index == -1) ? index : dataGridViewRow.Index, args2);
         }
         else
         {
             DataGridViewCell dataGridViewCell = element as DataGridViewCell;
             if (dataGridViewCell != null)
             {
                 DataGridViewCellStateChangedEventArgs args3 = new DataGridViewCellStateChangedEventArgs(dataGridViewCell, elementState);
                 this.OnCellStateChanged(args3);
             }
         }
     }
     if ((elementState & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected)
     {
         if (this.noSelectionChangeCount > 0)
         {
             this.dataGridViewState2[0x40000] = true;
         }
         else
         {
             this.OnSelectionChanged(EventArgs.Empty);
         }
     }
 }