0

そのため、ここでいくつかの投稿を見て、すべての解決策を試してみましたが、役に立ちませんでした。ウェブ全体でいくつかの例を試しましたが、何も機能しませんでした。それは私をからかっている !以下のコードは、私が現在実行しているものであり、動作するはずですが、動作しません。問題は、値が 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 = "";
        }
4

2 に答える 2

2

特にこの問題の正しい解決策ではないかもしれませんが、セルチェック値を取得するために役立ちました:

CellClick の代わりに CellContentClick イベントを使用します。最初のものはチェックが正しくクリックされた場合にのみトリガーされ、2番目のものはセルの任意の部分をクリックしたときにトリガーされますが、これは問題です。また、何らかの理由で DataGridViewCheckBoxCell.EditedFormattedValue が CellClick で間違った値を返すため、EditedFormattedValue を使用します。

次のコードを使用します。

DataGridViewCheckBoxCell currentCell = (DataGridViewCheckBoxCell)dataGridView.CurrentCell;
if ((bool)currentCell.EditedFormattedValue)
     //do sth
else
     //do sth
于 2018-01-26T16:44:21.097 に答える
0
DataGridView.Rows[0].Cells[0].Value = true; 
or 
DataGridView.Rows[0].Cells[0].Value = false; 
于 2014-05-08T08:49:10.747 に答える