问题
I am new to posting on Stack. I have searched for quite some time to a problem similar to mine. I am trying to change the checkboxes in a WinForms DataGridView from not read-only to read-only based on the object's boolean value dynamically.
It is showing in debug mode that the change has happened but once it fully runs through, the checkbox cells that are supposed to be read only are still allowing check and uncheck functionality. I have left the commented out section in to show that I have attempted to do this.
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();
Any help would be greatly appreciated.
回答1:
From the line m_SingletonForm.ShowDialog();
it appears that you have this code before the DataGridView
has been displayed *. This is too early for such changes to the grid items to be applied. You would also see the same issue if your code was inside the constructor for your form.
The simplest fix for the issue is to put you code for setting the cells to readonly within a DataBindingComplete
event handler. Something like this:
// 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;
}
}
}
* I have never 100% worked out why it is this way - I believe it is related to the fact that there are two sets of cells in the DataGridView
- the editing/ui cells and the data they sit upon.
来源:https://stackoverflow.com/questions/13502071/using-a-dynamic-list-of-custom-object-and-cant-dynamically-change-datagrids-ce