そのため、ここでいくつかの投稿を見て、すべての解決策を試してみましたが、役に立ちませんでした。ウェブ全体でいくつかの例を試しましたが、何も機能しませんでした。それは私をからかっている !以下のコードは、私が現在実行しているものであり、動作するはずですが、動作しません。問題は、値が true または false でない場合、値が {} として表示されるため、無効なキャストで爆発することです。値が true の場合、cell.Value = cell.TrueValue の場合に true として識別されることはありません。datagridview セットアップで TrueValue と FalseValue をそれぞれ true と false に設定しました。私は何を逃したのですか?
DataGridViewCheckBoxCell cell =
(DataGridViewCheckBoxCell) ((DataGridView) sender).Rows[e.RowIndex].Cells[e.ColumnIndex];
if (cell.ValueType == typeof(bool))
{
if (cell.Value != null && !(bool)cell.Value)
cell.Value = cell.TrueValue;
else
cell.Value = cell.FalseValue;
}
やっと参加できたと思います。cell.Value == DBNull.Value 新しいバージン チェックボックスの値。cell.Value == cell.FalseValue はまだ機能していません。
更新されたコード
if (cell.ValueType == typeof (bool))
{
if (cell.Value == DBNull.Value || cell.Value == cell.FalseValue)
{
cell.Value = cell.TrueValue;
}
else if ((bool)cell.Value)
{
cell.Value = cell.FalseValue;
}
}
私はついにそれを釘付けにしました。最後の問題は、Convert.ToBoolean(cell.Value) == false ではなく cell.Value == cell.FalseValue を使用することで解決されました。
最終コード:
DataGridViewCheckBoxCell cell =
(DataGridViewCheckBoxCell)((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex];
if (cell.ValueType != typeof (bool)) return;
if (cell.Value == DBNull.Value || Convert.ToBoolean(cell.Value) == false)
{
cell.Value = cell.TrueValue;
((DataGridView)sender).Rows[e.RowIndex].Cells["Comment"].Value = "Not in source.";
}
else
{
cell.Value = cell.FalseValue;
((DataGridView)sender).Rows[e.RowIndex].Cells["Comment"].Value = "";
}