当更改CheckBox是空格键时,AcceptChanges()会抛出异常 [英] AcceptChanges() throws exception when changing CheckBox is with space bar
问题描述
在我的 DataGridView
中,我使用DataView过滤DataTable。在过滤器中使用 CheckBox
值。
In my DataGridView
I use a DataView to filter the DataTable. The CheckBox
value is used in the filter.
当复选框未被选中时,行应该消失。要立即运行,我在 CurrentCellDirtyStateChanged
事件中使用 AcceptChanges()
。 (否则行保持显示,直到选择另一行)。
When the CheckBox is unchecked, the row should disappear. To run that immediately, I use AcceptChanges()
in an CurrentCellDirtyStateChanged
event. (Otherwise the row stays displayed, until another row is selected).
当我用鼠标取消选中该复选框时,这可以工作。 使用空格键将抛出 NullReferenceException 异常。
以下是一些示例代码:
(完全正常工作,只需要一个空格为DataGridView1的Form1,用空格键更改复选框会引发异常)
(Full working. Needs only Form1 with a blank DataGridView1. Changing the CheckBox with space bar throws the exception)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
DataTable table;
DataView view;
public Form1()
{
InitializeComponent();
Init();
}
// Building the table and view
private void Init()
{
table = new DataTable();
table.Columns.Add("check", typeof(bool));
table.Rows.Add(true);
table.Rows.Add(true);
table.Rows.Add(true);
view = new DataView(table);
view.RowFilter = "check = true";
dataGridView1.DataSource = view;
}
// CurrentCellDirtyStateChanged Event
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty == true && dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
// AcceptChanges to update the view
// works with mouse click, throws NullReferenceException when spacebar is used
table.AcceptChanges();
}
}
}
}
有没有办法使它与空格键一起工作?
Is there any way make it working with the space bar also?
修改
异常被抛出在.net运行时,而不是直接由AcceptChanges()
Edit
The exception is thrown anywhere in the .net runtime and not directly by AcceptChanges()
System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
bei System.Windows.Forms.DataGridViewCheckBoxCell.NotifyMASSClient(Point position)
bei System.Windows.Forms.DataGridViewCheckBoxCell.OnKeyUp(KeyEventArgs e, Int32 rowIndex)
bei System.Windows.Forms.DataGridView.OnKeyUp(KeyEventArgs e)
bei System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
bei System.Windows.Forms.DataGridView.ProcessKeyEventArgs(Message& m)
bei System.Windows.Forms.Control.WmKeyChar(Message& m)
bei System.Windows.Forms.Control.WndProc(Message& m)
bei System.Windows.Forms.DataGridView.WndProc(Message& m)
bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
bei Sample.Program.Main() in C:\Projects\TFS\Sample\Sample\Program.cs:Zeile 21.
推荐答案
如 Microsoft Connect 一个潜在的解决方法是延迟 CommitEdit
直到 CellDirtyStateChanged
事件处理程序完成。
As reported on Microsoft Connect a potential workaround is to delay CommitEdit
until CellDirtyStateChanged
event handler completes.
public delegate void InvokeDelegate();
public void MyDelegate()
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
table.AcceptChanges();
}
//CurrentCellDirtyStateChanged Event
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty == true && dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
{
BeginInvoke(new InvokeDelegate(MyDelegate));
}
}
这篇关于当更改CheckBox是空格键时,AcceptChanges()会抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!