AcceptChanges() throws exception when changing CheckBox is with space bar

懵懂的女人 提交于 2019-12-12 15:41:40

问题


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 should 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 some sample code:

(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?

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.

回答1:


Using the Invoke this way makes for me the code easier to read, because there is no explicit delegate declaration needed.

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.




回答2:


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


来源:https://stackoverflow.com/questions/28590324/acceptchanges-throws-exception-when-changing-checkbox-is-with-space-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!