当复选框被改变与空格键的AcceptChanges()抛出异常 [英] AcceptChanges() throws exception when CheckBox is changed with space bar
问题描述
在我的DataGridView我使用DataView过滤数据表。复选框值用于在过滤器
在该复选框被选中,该行必须消失。要立即运行,我用的AcceptChanges()在CurrentCellDirtyStateChanged事件。 (否则,该行保持显示,直到另一行被选中)。
这工作时,我不选择用鼠标的复选框。使用空格键一个NullReferenceException异常。
下面是一个简单的code:
(全部工作,只有Form1的空白dataGridView1的。改变与空格键的复选框抛出异常)
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;
使用System.Windows.Forms的;
命名空间WindowsFormsApplication1
{
公共部分类Form1中:形态
{
数据表表;
数据视图查看;
公共Form1中()
{
的InitializeComponent();
在里面();
}
//构建表和视图
私人无效的init()
{
表=新的DataTable();
table.Columns.Add(选中的typeof(布尔));
table.Rows.Add(真正的);
table.Rows.Add(真正的);
table.Rows.Add(真正的);
鉴于=新的数据视图(表);
view.RowFilter =检查=真;
dataGridView1.DataSource =图。
}
// CurrentCellDirtyStateChanged事件
私人无效dataGridView1_CurrentCellDirtyStateChanged(对象发件人,EventArgs的)
{
如果(dataGridView1.IsCurrentCellDirty ==真&功放;&安培; dataGridView1.CurrentCell是DataGridViewCheckBoxCell)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
// AcceptChanges的更新视图
//适用于鼠标点击,引发的NullReferenceException空格键时使用
table.AcceptChanges();
}
}
}
}
有什么办法让它在与空格键也?
修改
唯一的例外是通过AcceptChanges的任何地方扔在.NET运行库,而不是直接()
System.NullReferenceException:明镜Objektverweis wurde nicht奥夫EINE Objektinstanz festgelegt。
贝System.Windows.Forms.DataGridViewCheckBoxCell.NotifyMASSClient(点位)
贝System.Windows.Forms.DataGridViewCheckBoxCell.OnKeyUp(KeyEventArgs E,的Int32 rowIndex位置)
贝System.Windows.Forms.DataGridView.OnKeyUp(KeyEventArgs E)
贝System.Windows.Forms.Control.ProcessKeyEventArgs(消息和M)
贝System.Windows.Forms.DataGridView.ProcessKeyEventArgs(消息和M)
贝System.Windows.Forms.Control.WmKeyChar(消息和M)
贝System.Windows.Forms.Control.WndProc(消息和M)
贝System.Windows.Forms.DataGridView.WndProc(消息和M)
贝System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr的的HWND,味精的Int32,IntPtr的WPARAM,IntPtr的LPARAM)
贝System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG和放大器; MSG)
贝System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,的Int32原因的Int32 pvLoopData)
贝System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(的Int32原因,ApplicationContext的情况下)
贝System.Windows.Forms.Application.ThreadContext.RunMessageLoop(的Int32原因,ApplicationContext的情况下)
贝Sample.Program.Main()在C:\项目\ TFS \样本\样本\ Program.cs的:Zeile 21。
据报道在<一个href="https://connect.microsoft.com/VisualStudio/feedback/details/780347/nullreferenceexception-in-notifymassclient-after-checking-unchecking-a-checkbox-in-datagridview-with-spacebar"相对=nofollow> Microsoft连接的一个潜在的解决方法是推迟 commitEdit的
直到 CellDirtyStateChanged
事件处理程序完成
公共委托无效InvokeDelegate();
公共无效MyDelegate()
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
table.AcceptChanges();
}
// CurrentCellDirtyStateChanged事件
私人无效dataGridView1_CurrentCellDirtyStateChanged(对象发件人,EventArgs的)
{
如果(dataGridView1.IsCurrentCellDirty ==真&功放;&安培; dataGridView1.CurrentCell是DataGridViewCheckBoxCell)
{
的BeginInvoke(新InvokeDelegate(MyDelegate));
}
}
In my DataGridView I use a DataView to filter the DataTable. The CheckBox value is used in the filter.
When the CheckBox is unchecked, the row must disappear. To run that immediately, I use AcceptChanges() in an CurrentCellDirtyStateChanged event. (Otherwise the row stays displayed, until another row is selected).
This works when I unselect the checkbox with the mouse. Using the space bar a NullReferenceException exception is thrown.
Here is a sample code:
(Full working. Only Form1 with 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?
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.
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));
}
}
这篇关于当复选框被改变与空格键的AcceptChanges()抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!