Skip to content

Commit

Permalink
Enable nullability in some DataGridView.Methods members (dotnet#10511)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetrou authored Dec 28, 2023
1 parent 6e73cf8 commit 25fd629
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19310,7 +19310,7 @@ internal bool OnSortCompare(
int rowIndex2,
out int sortResult)
{
DataGridViewSortCompareEventArgs dgvsce = new DataGridViewSortCompareEventArgs(
DataGridViewSortCompareEventArgs dgvsce = new(
dataGridViewSortedColumn,
value1,
value2,
Expand Down Expand Up @@ -20174,7 +20174,7 @@ private void PerformLayoutPrivate(
Debug.Assert(_inPerformLayoutCount >= 0);
}
}
#nullable disable

private void PopulateNewRowWithDefaultValues()
{
if (NewRowIndex != -1)
Expand Down Expand Up @@ -20253,12 +20253,16 @@ private void PositionEditingControl(bool setLocation, bool setSize, bool setFocu

cellClip.Intersect(editingZone);

if (_editingPanel is null)
{
throw new InvalidOperationException();
}

if (cellClip.Width == 0 || cellClip.Height == 0)
{
// we cannot simply make the control invisible because we want it to keep the focus.
// (and Control::CanFocus returns false if the control is not visible).
// So we place the editing control to the right of the DataGridView.
Debug.Assert(EditingControl is not null);
_editingPanel.Location = new Point(Width + 1, 0);
_dataGridViewState1[State1_EditingControlHidden] = true;
}
Expand Down Expand Up @@ -20289,12 +20293,21 @@ private void PositionEditingControl(bool setLocation, bool setSize, bool setFocu
cellClip.Height++;
}

if (InheritedEditingCellStyle is null)
{
throw new InvalidOperationException();
}

CurrentCellInternal.PositionEditingControl(
setLocation || _dataGridViewState1[State1_EditingControlHidden],
setSize || _dataGridViewState1[State1_EditingControlHidden],
cellBounds, cellClip, InheritedEditingCellStyle,
singleVerticalBorderAdded, singleHorizontalBorderAdded,
isFirstDisplayedColumn, isFirstDisplayedRow);
cellBounds,
cellClip,
InheritedEditingCellStyle,
singleVerticalBorderAdded,
singleHorizontalBorderAdded,
isFirstDisplayedColumn,
isFirstDisplayedRow);
_dataGridViewState1[State1_EditingControlHidden] = false;
}

Expand Down Expand Up @@ -20359,7 +20372,9 @@ protected bool ProcessDeleteKey(Keys keyData)
int dataGridRowsCount = Rows.Count;
#if DEBUG
int dataGridViewRowsCount = Rows.Count; // the number of rows in the dataGridView row collection not counting the AddNewRow
int rowCount = DataConnection.CurrencyManager.List.Count;

// We already check inside DataSource if DataConnection is null.
int rowCount = DataConnection!.CurrencyManager?.List.Count ?? 0;
if (AllowUserToAddRowsInternal)
{
if (NewRowIndex < rowCount)
Expand All @@ -20375,10 +20390,11 @@ protected bool ProcessDeleteKey(Keys keyData)

Debug.Assert(rowCount == dataGridViewRowsCount, "out of sync");
#endif
DataGridViewDataErrorEventArgs dgvdee = null;
DataGridViewDataErrorEventArgs? dgvdee = null;
try
{
DataConnection.DeleteRow(rowIndex);
// We already check inside DataSource if DataConnection is null.
DataConnection!.DeleteRow(rowIndex);
}
catch (Exception exception) when (!exception.IsCriticalException())
{
Expand Down Expand Up @@ -20416,7 +20432,7 @@ protected bool ProcessDeleteKey(Keys keyData)
}
else
{
Rows.RemoveAtInternal(rowIndex, false /*force*/);
Rows.RemoveAtInternal(rowIndex, force: false);
Debug.Assert(dataGridViewRow.Index == -1);
DataGridViewRowEventArgs dgvre = new DataGridViewRowEventArgs(dataGridViewRow);
OnUserDeletedRow(dgvre);
Expand Down Expand Up @@ -20503,16 +20519,17 @@ protected override bool ProcessDialogKey(Keys keyData)
if (EditingControl is not null)
{
_dataGridViewState1[State1_LeavingWithTabKey] = true;
if (!EndEdit(DataGridViewDataErrorContexts.Parsing | DataGridViewDataErrorContexts.Commit | DataGridViewDataErrorContexts.LeaveControl,
DataGridViewValidateCellInternal.Always,
true /*fireCellLeave*/,
true /*fireCellEnter*/,
true /*fireRowLeave*/,
true /*fireRowEnter*/,
true /*fireLeave*/,
false /*keepFocus*/,
false /*resetCurrentCell*/,
false /*resetAnchorCell unused here*/))
if (!EndEdit(
DataGridViewDataErrorContexts.Parsing | DataGridViewDataErrorContexts.Commit | DataGridViewDataErrorContexts.LeaveControl,
DataGridViewValidateCellInternal.Always,
fireCellLeave: true,
fireCellEnter: true,
fireRowLeave: true,
fireRowEnter: true,
fireLeave: true,
keepFocus: false,
resetCurrentCell: false,
resetAnchorCell: false))
{
return true;
}
Expand Down Expand Up @@ -20545,7 +20562,7 @@ protected bool ProcessDownKey(Keys keyData)
private bool ProcessDownKeyInternal(Keys keyData, out bool moved)
{
bool success;
DataGridViewColumn dataGridViewColumn = Columns.GetFirstColumn(DataGridViewElementStates.Visible);
DataGridViewColumn? dataGridViewColumn = Columns.GetFirstColumn(DataGridViewElementStates.Visible);
int firstVisibleColumnIndex = (dataGridViewColumn is null) ? -1 : dataGridViewColumn.Index;
int lastVisibleRowIndex = Rows.GetLastRow(DataGridViewElementStates.Visible);
if (firstVisibleColumnIndex == -1 || lastVisibleRowIndex == -1)
Expand Down Expand Up @@ -20782,7 +20799,7 @@ private bool ProcessDownKeyInternal(Keys keyData, out bool moved)
return true;
}

if (!ScrollIntoView(_ptCurrentCell.X, nextVisibleRowIndex, true /*forCurrentCellChange*/))
if (!ScrollIntoView(_ptCurrentCell.X, nextVisibleRowIndex, forCurrentCellChange: true))
{
return true;
}
Expand All @@ -20795,11 +20812,12 @@ private bool ProcessDownKeyInternal(Keys keyData, out bool moved)

ClearSelection();
SetSelectedCellCore(_ptCurrentCell.X, nextVisibleRowIndex, true);
success = SetCurrentCellAddressCore(_ptCurrentCell.X,
success = SetCurrentCellAddressCore(
_ptCurrentCell.X,
nextVisibleRowIndex,
true /*setAnchorCellAddress*/,
false /*validateCurrentCell*/,
false /*throughMouseClick*/);
setAnchorCellAddress: true,
validateCurrentCell: false,
throughMouseClick: false);
if (!success)
{
moved = false;
Expand Down Expand Up @@ -21445,7 +21463,8 @@ private bool ProcessDownKeyInternal(Keys keyData, out bool moved)

protected bool ProcessEndKey(Keys keyData)
{
DataGridViewColumn dataGridViewColumn = Columns.GetLastColumn(DataGridViewElementStates.Visible,
DataGridViewColumn? dataGridViewColumn = Columns.GetLastColumn(
DataGridViewElementStates.Visible,
DataGridViewElementStates.None);
int lastVisibleColumnIndex = (dataGridViewColumn is null) ? -1 : dataGridViewColumn.Index;
int firstVisibleRowIndex = Rows.GetFirstRow(DataGridViewElementStates.Visible);
Expand Down Expand Up @@ -21773,21 +21792,22 @@ protected bool ProcessEnterKey(Keys keyData)

if (!moved)
{
DataGridViewCell dataGridViewCurrentCell = null;
DataGridViewCell? dataGridViewCurrentCell = null;
// Try to commit the potential editing
if (EditMode == DataGridViewEditMode.EditOnEnter)
{
if (_ptCurrentCell.X != -1)
{
dataGridViewCurrentCell = CurrentCellInternal;
DataGridViewDataErrorEventArgs dgvdee = CommitEdit(ref dataGridViewCurrentCell,
DataGridViewDataErrorEventArgs? dgvdee = CommitEdit(
ref dataGridViewCurrentCell,
DataGridViewDataErrorContexts.Parsing | DataGridViewDataErrorContexts.Commit,
DataGridViewValidateCellInternal.WhenChanged,
false /*fireCellLeave*/,
false /*fireCellEnter*/,
false /*fireRowLeave*/,
false /*fireRowEnter*/,
false /*fireLeave*/);
fireCellLeave: false,
fireCellEnter: false,
fireRowLeave: false,
fireRowEnter: false,
fireLeave: false);
if (dgvdee is not null)
{
if (dgvdee.ThrowException)
Expand All @@ -21799,16 +21819,17 @@ protected bool ProcessEnterKey(Keys keyData)
}
else
{
EndEdit(DataGridViewDataErrorContexts.Parsing | DataGridViewDataErrorContexts.Commit,
DataGridViewValidateCellInternal.WhenChanged /*validateCell*/,
false /*fireCellLeave*/,
false /*fireCellEnter*/,
false /*fireRowLeave*/,
false /*fireRowEnter*/,
false /*fireLeave*/,
true /*keepFocus*/,
true /*resetCurrentCell unused here*/,
true /*resetAnchorCell unused here*/);
EndEdit(
DataGridViewDataErrorContexts.Parsing | DataGridViewDataErrorContexts.Commit,
validateCell: DataGridViewValidateCellInternal.WhenChanged,
fireCellLeave: false,
fireCellEnter: false,
fireRowLeave: false,
fireRowEnter: false,
fireLeave: false,
keepFocus: true,
resetCurrentCell: true,
resetAnchorCell: true);
}

if (/*commitRow && */IsCurrentRowDirty)
Expand Down Expand Up @@ -21846,7 +21867,7 @@ protected bool ProcessEscapeKey(Keys keyData)
}
else
{
CancelEdit(true /*endEdit, DataGridViewDataErrorContexts.Parsing | DataGridViewDataErrorContexts.InitialValueRestoration*/);
CancelEdit(endEdit: true /*, DataGridViewDataErrorContexts.Parsing | DataGridViewDataErrorContexts.InitialValueRestoration*/);
}

return true;
Expand All @@ -21869,9 +21890,9 @@ protected bool ProcessF2Key(Keys keyData)
&& !IsSharedCellReadOnly(CurrentCellInternal, _ptCurrentCell.Y)
&& (EditMode == DataGridViewEditMode.EditOnKeystrokeOrF2 || EditMode == DataGridViewEditMode.EditOnF2))
{
bool success = ScrollIntoView(_ptCurrentCell.X, _ptCurrentCell.Y, false);
bool success = ScrollIntoView(_ptCurrentCell.X, _ptCurrentCell.Y, forCurrentCellChange: false);
Debug.Assert(success);
BeginEditInternal(EditMode == DataGridViewEditMode.EditOnF2 /*selectAll*/);
BeginEditInternal(selectAll: EditMode == DataGridViewEditMode.EditOnF2);
return true;
}
}
Expand Down Expand Up @@ -21904,7 +21925,7 @@ protected bool ProcessF3Key(Keys keyData)

protected bool ProcessHomeKey(Keys keyData)
{
DataGridViewColumn dataGridViewColumn = Columns.GetFirstColumn(DataGridViewElementStates.Visible);
DataGridViewColumn? dataGridViewColumn = Columns.GetFirstColumn(DataGridViewElementStates.Visible);
int firstVisibleColumnIndex = (dataGridViewColumn is null) ? -1 : dataGridViewColumn.Index;
int firstVisibleRowIndex = Rows.GetFirstRow(DataGridViewElementStates.Visible);
if (firstVisibleColumnIndex == -1 || firstVisibleRowIndex == -1)
Expand Down Expand Up @@ -22209,7 +22230,7 @@ protected bool ProcessInsertKey(Keys keyData)
&& (keyData & Keys.KeyCode) == Keys.C))
&& ClipboardCopyMode != DataGridViewClipboardCopyMode.Disable)
{
DataObject dataObject = GetClipboardContent();
DataObject? dataObject = GetClipboardContent();
if (dataObject is not null)
{
Clipboard.SetDataObject(dataObject);
Expand Down Expand Up @@ -22237,8 +22258,8 @@ protected override bool ProcessKeyEventArgs(ref Message m)
KeyEventArgs ke = new KeyEventArgs((Keys)(nint)m.WParamInternal | ModifierKeys);
if (ke.KeyCode != Keys.ProcessKey || m.LParamInternal != 0x01) // Changing IME context does not trigger editing mode
{
Type editControlType = dataGridViewCell.EditType;
Type editingCellInterface = null;
Type? editControlType = dataGridViewCell.EditType;
Type? editingCellInterface = null;
if (editControlType is null)
{
// Current cell does not have an editing control. Does it implement IDataGridViewEditingCell?
Expand Down Expand Up @@ -22278,7 +22299,7 @@ protected override bool ProcessKeyEventArgs(ref Message m)

return base.ProcessKeyEventArgs(ref m);
}

#nullable disable
protected override bool ProcessKeyPreview(ref Message m)
{
bool dataGridViewWantsInputKey;
Expand Down Expand Up @@ -23765,7 +23786,8 @@ private bool ProcessRightKeyPrivate(Keys keyData)
}

bool success;
DataGridViewColumn dataGridViewColumn = Columns.GetLastColumn(DataGridViewElementStates.Visible,
DataGridViewColumn dataGridViewColumn = Columns.GetLastColumn(
DataGridViewElementStates.Visible,
DataGridViewElementStates.None);
int lastVisibleColumnIndex = (dataGridViewColumn is null) ? -1 : dataGridViewColumn.Index;
int firstVisibleRowIndex = Rows.GetFirstRow(DataGridViewElementStates.Visible);
Expand Down Expand Up @@ -28891,7 +28913,8 @@ private bool TabToPreviousCell()
previousVisibleRowIndex = Rows.GetPreviousRow(_ptCurrentCell.Y, DataGridViewElementStates.Visible);
}

dataGridViewColumn = Columns.GetLastColumn(DataGridViewElementStates.Visible,
dataGridViewColumn = Columns.GetLastColumn(
DataGridViewElementStates.Visible,
DataGridViewElementStates.None);
ThrowInvalidOperationExceptionIfNull(dataGridViewColumn);
int lastVisibleColumnIndex = dataGridViewColumn.Index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public override void DoDefaultAction()
else if (dataGridView.EditMode != DataGridViewEditMode.EditProgrammatically)
{
// start editing
dataGridView.BeginEdit(true /*selectAll*/);
dataGridView.BeginEdit(selectAll: true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ OwningColumn is null ||
cellStyle,
dgvabsEffective,
DataGridViewPaintParts.ContentForeground,
false /*computeContentBounds*/,
true /*computeErrorIconBounds*/,
false /*paint*/);
computeContentBounds: false,
computeErrorIconBounds: true,
paint: false);
Debug.Assert(errBoundsDebug.Equals(errBounds));
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override Rectangle Bounds
return Rectangle.Empty;
}

Rectangle rowRect = _owningDataGridViewRow.DataGridView.RectangleToScreen(_owningDataGridViewRow.DataGridView.GetRowDisplayRectangle(_owningDataGridViewRow.Index, false /*cutOverflow*/));
Rectangle rowRect = _owningDataGridViewRow.DataGridView.RectangleToScreen(_owningDataGridViewRow.DataGridView.GetRowDisplayRectangle(_owningDataGridViewRow.Index, cutOverflow: false));

int horizontalScrollBarHeight = 0;
if (_owningDataGridViewRow.DataGridView.HorizontalScrollBarVisible)
Expand Down Expand Up @@ -196,7 +196,7 @@ public override AccessibleStates State

if (_owningDataGridViewRow.DataGridView is not null && _owningDataGridViewRow.DataGridView.IsHandleCreated)
{
Rectangle rowBounds = _owningDataGridViewRow.DataGridView.GetRowDisplayRectangle(_owningDataGridViewRow.Index, true /*cutOverflow*/);
Rectangle rowBounds = _owningDataGridViewRow.DataGridView.GetRowDisplayRectangle(_owningDataGridViewRow.Index, cutOverflow: true);
if (!rowBounds.IntersectsWith(_owningDataGridViewRow.DataGridView.ClientRectangle))
{
accState |= AccessibleStates.Offscreen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override Rectangle Bounds
return Rectangle.Empty;
}

Rectangle cellRect = Owner.DataGridView.GetCellDisplayRectangle(-1, -1, false /*cutOverflow*/);
Rectangle cellRect = Owner.DataGridView.GetCellDisplayRectangle(-1, -1, cutOverflow: false);
return Owner.DataGridView.RectangleToScreen(cellRect);
}
}
Expand Down Expand Up @@ -117,7 +117,7 @@ public override AccessibleStates State
}

// If all the cells are selected, then the top left header cell accessible object is considered to be selected as well.
if (Owner.DataGridView is not null && Owner.DataGridView.AreAllCellsSelected(false /*includeInvisibleCells*/))
if (Owner.DataGridView is not null && Owner.DataGridView.AreAllCellsSelected(includeInvisibleCells: false))
{
resultState |= AccessibleStates.Selected;
}
Expand Down

0 comments on commit 25fd629

Please sign in to comment.