Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ internal static partial class LocalAppContextSwitches
internal const string ServicePointManagerCheckCrlSwitchName = "System.Windows.Forms.ServicePointManagerCheckCrl";
internal const string TrackBarModernRenderingSwitchName = "System.Windows.Forms.TrackBarModernRendering";
private const string DoNotCatchUnhandledExceptionsSwitchName = "System.Windows.Forms.DoNotCatchUnhandledExceptions";
internal const string DataGridViewUIAStartRowCountAtOneSwitchName = "System.Windows.Forms.DataGridViewUIAStartRowCountAtOne";

private static int s_scaleTopLevelFormMinMaxSizeForDpi;
private static int s_anchorLayoutV2;
private static int s_servicePointManagerCheckCrl;
private static int s_trackBarModernRendering;
private static int s_doNotCatchUnhandledExceptions;
private static int s_dataGridViewUIAStartRowCountAtOne;

private static FrameworkName? s_targetFrameworkName;

Expand Down Expand Up @@ -151,4 +153,10 @@ public static bool ServicePointManagerCheckCrl
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetCachedSwitchValue(ServicePointManagerCheckCrlSwitchName, ref s_servicePointManagerCheckCrl);
}

public static bool DataGridViewUIAStartRowCountAtOne
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetCachedSwitchValue(DataGridViewUIAStartRowCountAtOneSwitchName, ref s_dataGridViewUIAStartRowCountAtOne);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms.Primitives;
using static Interop;

namespace System.Windows.Forms;
Expand Down Expand Up @@ -55,9 +56,9 @@ public override string? Name

int rowIndex = _owner.DataGridView is null
? -1
: _owner.DataGridView.Rows.GetVisibleIndex(_owner.OwningRow);
: _owner.DataGridView.Rows.GetVisibleIndex(_owner.OwningRow) + RowStartIndex;

string name = string.Format(SR.DataGridView_AccDataGridViewCellName, _owner.OwningColumn.HeaderText, rowIndex);
string name = string.Format(SR.DataGridView_AccDataGridViewCellName, _owner.OwningColumn.HeaderText, rowIndex).Trim();

if (_owner.OwningColumn.SortMode != DataGridViewColumnSortMode.NotSortable)
{
Expand Down Expand Up @@ -113,6 +114,8 @@ private AccessibleObject? ParentPrivate

public override AccessibleRole Role => AccessibleRole.Cell;

private static int RowStartIndex => LocalAppContextSwitches.DataGridViewUIAStartRowCountAtOne ? 1 : 0;

public override AccessibleStates State
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Drawing;
using System.Globalization;
using System.Text;
using System.Windows.Forms.Primitives;
using static Interop;

namespace System.Windows.Forms;
Expand Down Expand Up @@ -94,7 +95,7 @@ public override string Name
}

int index = _owningDataGridViewRow is { Visible: true, DataGridView: { } }
? _owningDataGridViewRow.DataGridView.Rows.GetVisibleIndex(_owningDataGridViewRow)
? _owningDataGridViewRow.DataGridView.Rows.GetVisibleIndex(_owningDataGridViewRow) + RowStartIndex
: -1;

return string.Format(SR.DataGridView_AccRowName, index.ToString(CultureInfo.CurrentCulture));
Expand Down Expand Up @@ -132,6 +133,8 @@ private AccessibleObject? ParentPrivate

public override AccessibleRole Role => AccessibleRole.Row;

private static int RowStartIndex => LocalAppContextSwitches.DataGridViewUIAStartRowCountAtOne ? 1 : 0;

internal override int[] RuntimeId
=> _runtimeId ??= new int[]
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Drawing;
using System.Windows.Forms.Primitives;
using Moq;
using Moq.Protected;
using static Interop;
Expand Down Expand Up @@ -1432,6 +1433,23 @@ private DataGridView CreateDataGridView(int columnCount, bool createControl = tr
return dataGridView;
}

// Unit test for https://github.com/dotnet/winforms/issues/7154
[WinFormsFact]
public void DataGridView_SwitchConfigured_AdjustsCellRowStartIndices()
{
AppContext.SetSwitch(LocalAppContextSwitches.DataGridViewUIAStartRowCountAtOneSwitchName, true);

using DataGridView dataGridView = new();
dataGridView.Columns.Add(new DataGridViewTextBoxColumn());
dataGridView.Rows.Add(new DataGridViewRow());

Assert.Equal($"{string.Format(SR.DataGridView_AccRowName, 1)}, Not sorted.", dataGridView.Rows[0].Cells[0].AccessibilityObject.Name);

AppContext.SetSwitch(LocalAppContextSwitches.DataGridViewUIAStartRowCountAtOneSwitchName, false);

Assert.Equal($"{string.Format(SR.DataGridView_AccRowName, 0)}, Not sorted.", dataGridView.Rows[0].Cells[0].AccessibilityObject.Name);
}

private class SubDataGridViewCell : DataGridViewCell
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Drawing;
using System.Windows.Forms.Primitives;
using static Interop;

namespace System.Windows.Forms.Tests;
Expand Down Expand Up @@ -2379,6 +2380,23 @@ public void DataGridViewRowAccessibleObject_GetPropertyValue_ValueValuePropertyI
Assert.False(dataGridView.IsHandleCreated);
}

// Unit test for https://github.com/dotnet/winforms/issues/7154
[WinFormsFact]
public void DataGridView_SwitchConfigured_AdjustsRowStartIndices()
{
AppContext.SetSwitch(LocalAppContextSwitches.DataGridViewUIAStartRowCountAtOneSwitchName, true);

using DataGridView dataGridView = new();
dataGridView.Columns.Add(new DataGridViewTextBoxColumn());
dataGridView.Rows.Add(new DataGridViewRow());

Assert.Equal(string.Format(SR.DataGridView_AccRowName, 1), dataGridView.Rows[0].AccessibilityObject.Name);

AppContext.SetSwitch(LocalAppContextSwitches.DataGridViewUIAStartRowCountAtOneSwitchName, false);

Assert.Equal(string.Format(SR.DataGridView_AccRowName, 0), dataGridView.Rows[0].AccessibilityObject.Name);
}

private class SubDataGridViewCell : DataGridViewCell
{
}
Expand Down