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.