Uses a dynamic list of a user object and cannot dynamically change the properties of dataGrid cells

I am new to publishing on Stack. I searched for quite a while a problem similar to mine. I am trying to change the checkboxes in WinForms DataGridView from not only read, but read only based on the dynamic value of the object.

It shows in debug mode that this happened, but as soon as it passes completely, the flag cells, which should only be read, still allow you to check and cancel the functionality. I left a commented section to show that I tried 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.

+4
source share
1 answer

From the line m_SingletonForm.ShowDialog(); it seems like you have this code before the DataGridView displayed * . It is too early for such changes in the grid elements to be applied. You will also see the same problem if your code was inside the constructor for your form.

The simplest fix for this problem is to put the code for installing read-only cells in the DataBindingComplete event handler. Something like that:

 // 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 never found out why this happens - I believe that this is due to the fact that the DataGridView has two sets of cells - edit cells / ui and the data on which they are sitting.

+3
source

Source: https://habr.com/ru/post/1447598/


All Articles