4

スタックに投稿するのは初めてです。私は自分に似た問題をかなりの時間探しました。オブジェクトのブール値に基づいて、WinForms DataGridView のチェックボックスを読み取り専用から読み取り専用に動的に変更しようとしています。

変更が発生したことがデバッグ モードで示されていますが、完全に実行されると、読み取り専用であるはずのチェックボックス セルで引き続きチェックおよびチェック解除機能が許可されます。これを試みたことを示すために、コメントアウトされたセクションを残しました。

m_SingletonForm.dataGridView1.DataSource = list;
m_SingletonForm.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
m_SingletonForm.dataGridView1.Columns["StoreGroup"].ReadOnly = true;
m_SingletonForm.dataGridView1.Columns["Message"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
m_SingletonForm.dataGridView1[0, 0].ReadOnly = true;


foreach (DataGridViewRow row in m_SingletonForm.dataGridView1.Rows)
{
    //var isChecked = Convert.ToBoolean(row.Cells["SendFile"].Value);

    //if (!isChecked)
    //{
        //m_SingletonForm.dataGridView1.Rows[0].Cells["SendFile"].Style.BackColor = Color.Red;
        //m_SingletonForm.dataGridView1.Rows[0].Cells["SendFile"].ReadOnly = true;

        //m_SingletonForm.dataGridView1.Rows[row.Index].Cells["SendFile"].Style.BackColor = Color.Red;
        //m_SingletonForm.dataGridView1.Rows[row.Index].Cells["SendFile"].ReadOnly = true;
        //m_SingletonForm.dataGridView1["SendFile", row.Index].ReadOnly = true;
        //m_SingletonForm.dataGridView1["SendFile", row.Index].Style.BackColor = Color.Red;          
    // }
}

m_SingletonForm.label1.Text = message;
m_SingletonForm.Text = title;
MessageBox.Show(m_SingletonForm.dataGridView1[0, 0].ReadOnly.ToString());
m_SingletonForm.ShowDialog();

どんな助けでも大歓迎です。

4

1 に答える 1

3

行から、が表示されるm_SingletonForm.ShowDialog();前にこのコードがあるように見えます*。これは、グリッド項目へのそのような変更を適用するには時期尚早です。コードがフォームのコンストラクター内にある場合も、同じ問題が発生します。DataGridView

DataBindingCompleteこの問題の最も簡単な解決策は、イベント ハンドラー内でセルを読み取り専用に設定するコードを配置することです。このようなもの:

// Attach the event
m_SingletonForm.dataGridView1.DataBindingComplete += new
    DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);


// And the code for the handler
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow row in m_SingletonForm.dataGridView1.Rows)
    {
        var isChecked = Convert.ToBoolean(row.Cells["SendFile"].Value);

        if (!isChecked)
        {
            m_SingletonForm.dataGridView1.Rows[0].Cells["SendFile"].Style.BackColor = Color.Red;
            m_SingletonForm.dataGridView1.Rows[0].Cells["SendFile"].ReadOnly = true;

            m_SingletonForm.dataGridView1.Rows[row.Index].Cells["SendFile"].Style.BackColor = Color.Red;
            m_SingletonForm.dataGridView1.Rows[row.Index].Cells["SendFile"].ReadOnly = true;
            m_SingletonForm.dataGridView1["SendFile", row.Index].ReadOnly = true;
            m_SingletonForm.dataGridView1["SendFile", row.Index].Style.BackColor = Color.Red;
        }
    }            
}

*なぜこのようになるのか 100% 解明したことはありませんDataGridView- 編集/UI セルとそれらが置かれているデータの 2 つのセットのセルがあるという事実に関連していると思います。

于 2012-11-22T13:33:48.013 に答える