LoginSignup
1
3

More than 5 years have passed since last update.

DataGridViewでチェックチェックボックスにチェックが付けられた(あるいはチェックが外された)ことを知ろうと思ったら例外が出たお話し

Posted at

発端→解決

DataGridViewでチェックが変更されたのを知りたい。

ググって発見。
DataGridViewの列にチェックボックスを表示する .NET Tips C#, VB.NET
・チェックボックスにチェックが付けられた(あるいはチェックが外された)ことを知る
https://dobon.net/vb/dotnet/datagridview/datagridviewcheckboxcolumn.html#section3

スペースキーでチェックボックスの値を変えると例外が出る。

ググって発見。デリゲートでCommitEditを遅延させる必要があるらしい。
https://connect.microsoft.com/VisualStudio/feedback/details/780347/nullreferenceexception-in-notifymassclient-after-checking-unchecking-a-checkbox-in-datagridview-with-spacebar

C#だったのでVB.NETへ変換。

Delegate Sub InvokeDelegate()

Private Sub MyDelegate()
    Me.DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End Sub

Private Sub DataGridView1_CurrentCellDirtyStateChanged(ByVal _sender As Object, ByVal _e As EventArgs)
    If Me.DataGridView1.CurrentCellAddress.X = 0 AndAlso
       Me.DataGridView1.IsCurrentCellDirty Then
        'コミットする
        BeginInvoke(New InvokeDelegate(AddressOf MyDelegate))
    End If
End Sub
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3