共用方式為


平板電腦上 DataGridView 控制項中的 StackOverflowException

本文可協助您避免在平板電腦上存取 DataGridView 控件時發生堆疊溢位例外狀況。

原始產品版本: .NET Framework
原始 KB 編號: 3046509

癥狀

當使用者第一次存取 System.Windows.Forms.DataGridView 在平板電腦上有許多數據列的控件時,控件會當機併產生堆疊溢位例外狀況。

如果第一個使用者動作是在應用程式啟動時存取控件底部附近的最後一列或另一個數據列,就會發生此問題。 例如,在應用程式啟動且顯示具有 Windows Forms DataGridView 控件的表單之後,用戶會捲動至控件的最後一列,然後選取該數據列。

如果第一個用戶動作是存取離 Windows Forms DataGridView 控件頂端不遠的數據列,則不會發生堆疊溢位。

以下是在當機後產生的呼叫堆疊範例:

System.Windows.Forms.dll!System.Windows.Forms.DataGridViewRow.DataGridViewRowAccessibleObject.Bounds.get()
<重複>
System.Windows.Forms.dll!System.Windows.Forms.DataGridViewRow.DataGridViewRowAccessibleObject.Bounds.get()
System.Windows.Forms.dll!System.Windows.Forms.DataGridViewRow.DataGridViewRowAccessibleObject.Bounds.get()
System.Windows.Forms.dll!System.Windows.Forms.DataGridViewCell.DataGridViewCellAccessibleObject.GetAccessibleObjectBounds(System.Windows.Forms.AccessibleObject parentAccObject)
System.Windows.Forms.dll!System.Windows.Forms.DataGridViewCell.DataGridViewCellAccessibleObject.Bounds.get()
System.Windows.Forms.dll!System.Windows.Forms.AccessibleObject.Accessibility.IAccessible.accLocation(int pxLeft, int pyTop, int pcxWidth, int pcyHeight, object childID)
System.Windows.Forms.dll!System.Windows.Forms.InternalAccessibleObject.System.Windows.Forms.UnsafeNativeMethods.IAccessibleInternal.accLocation(int l, int t, int w, int h, object childID)
[原生到受控轉換]
oleacc.dll!AccWrap_LocationEtcFix::accLocation(long *,long *,long *,long *,struct tagVARIANT)
tiptsf.dll!CARET::UpdateMSAAEditFieldState()
tiptsf.dll!CARET::UpdateEditFieldState(結構HWND__ *,unsigned int,unsigned int)
tiptsf.dll!CARET::_ProcessCaretEvents()
tiptsf.dll!CARET::P rocessCaretEvents()
user32.dll!___ClientCallWinEventProc@4-()
ntdll.dll!_KiUserCallbackDispatcher@12-()

原因

當使用者第一次存取 Windows Forms DataGridView 控件時,平板電腦輸入面板元件會查詢對應至 Windows Forms DataGridView 控件第一個子系的類別界限 AccessibleObject 。 一般而言,第一個子系是控件的最上層數據列。 不過,如果使用者向下卷動至控件底部,則最上層的數據列不會再顯示。

在此情況下,Windows Forms DataGridView 控件會嘗試傳回第一個可見的數據列。 為了尋找第一個可見的數據列,控件的 AccessibleObject 類別會遞歸列舉所有數據列,直到到達可見數據列為止。 根據進程可用的資源,程式在搜尋下一個數據列時可能會當機。

解決辦法

為了避免堆疊溢位例外狀況,開發人員可以在填入 Windows Forms DataGridView 控件時立即初始化 AccessibleObject 類別。 例如,您可以使用下列程式代碼來設定控制件的焦點:

dataGridView1.Focus();

當您執行此指令時,會發生下列事件序列:

  • 平板電腦輸入面板元件已初始化。
  • 平板電腦輸入面板元件會追蹤與控件相關聯的所有動作。
  • 平板電腦輸入面板元件會查詢所選數據列的界限,而不是第一個可見的數據列。

詳細資訊

這是 Windows Forms DataGridView 控件Microsoft Active Accessibility 支援中的實作流程。