I have a DataGridView that I use in a C # Winforms project. There are no auto-generating columns in the grid. I manually set the column names and properties.
This includes the DefaultCellStyle for cells. I want all cells of type DataGridViewCheckBoxColumn to be centered. I manually set this alignment for each column of this type in the designer. These changes are not displayed when loading a datagridview.
As my next approach, I used the code below in the DataBindingComplete event (which also had no effect)
foreach (DataGridViewColumn dgvc in this.dgv_Automations.Columns)
{
if(dgvc.CellType == typeof(DataGridViewCheckBoxCell))
dgvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
When debugging, the above code already set the alignment in the center. However, it still does not appear in the grid.
I CAN make it work if during the DataBindingComplete event I do something like this:
foreach (DataGridViewRow dgvr in this.dgv_Automations.Rows)
{
dgvr.Cells["isErrors"].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
However, I have a lot of these columns, and it really doesn't make sense to do it this way. EDIT: I implemented it this way, and with only 10 lines there is a very noticeable lag behind setting this property in each cell.
My question is: what is the correct way to do this? Why doesn't the DefaultCellStyle designer work?
Imreg source
share