简体   繁体   English

使用空格键更改 CheckBox 时,AcceptChanges() 在 DataGridView 中抛出 System.NullReferenceException

[英]AcceptChanges() throws System.NullReferenceException in DataGridView when using space bar for changing CheckBox

In my DataGridView I use a DataView to filter the DataTable.在我的DataGridView我使用 DataView 来过滤 DataTable。 The CheckBox value is used in the filter. CheckBox值用于过滤器。

When the CheckBox is unchecked, the row should disappear.当 CheckBox 未选中时,该行应该消失。 To run that immediately, I use AcceptChanges() in an CurrentCellDirtyStateChanged event.为了立即运行,我在CurrentCellDirtyStateChanged事件中使用AcceptChanges() (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.使用空格键会引发NullReferenceException异常。

Here is some sample code:下面是一些示例代码:

(Full working. Needs only Form1 with a blank DataGridView1. Changing the CheckBox with space bar throws the exception) (完整工作。只需要带有空白 DataGridView1 的 Form1。使用空格键更改 CheckBox 会引发异常)

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()异常在 .net 运行时的任何地方抛出,而不是直接由 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.

Using the Invoke this way makes for me the code easier to read, because there is no explicit delegate declaration needed.以这种方式使用 Invoke 使我的代码更易于阅读,因为不需要显式委托声明。

private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty == true && dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
    {
        // use BeginInvoke with (MethodInvoker) to run the code after the event is finished
        BeginInvoke((MethodInvoker)delegate
        {
            dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            table.AcceptChanges();
        });
    }
}

Works same like tezzos answer.像 tezzos 的回答一样工作。

As reported on Microsoft Connect a potential workaround is to delay CommitEdit until CellDirtyStateChanged event handler completes.正如Microsoft Connect上所报告的,一个潜在的解决方法是延迟CommitEdit直到CellDirtyStateChanged事件处理程序完成。

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));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 AzureCredentialsFactory.FromServicePrincipal 与证书一起使用会引发 System.NullReferenceException - Using AzureCredentialsFactory.FromServicePrincipal with a certificate throws System.NullReferenceException 单击按钮将引发System.NullReferenceException - Button Click throws System.NullReferenceException RegisterViewWithRegion 为视图抛出 System.NullReferenceException - RegisterViewWithRegion throws System.NullReferenceException for a View WebRequest.Create抛出System.NullReferenceException - WebRequest.Create throws System.NullReferenceException 使用空合并运算符时的 System.NullReferenceException - System.NullReferenceException when using null coalescing operator 在C#SOAP请求中使用WSDL时出现System.NullReferenceException - System.NullReferenceException when using WSDL in C# SOAP Request 单元测试时出现 System.NullReferenceException - System.NullReferenceException when unit testing System.NullReferenceException发布为Azure时 - System.NullReferenceException when published to azure System.NullReferenceException检查是否!= null - System.NullReferenceException When checking if != null 解析json时出现system.nullreferenceException - system.nullreferenceexception when parsing json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM