DataGridView单元格对齐将无法工作

ImG*_*reg 5 c# styles datagridview alignment winforms

我有一个我在C#Winforms项目中使用的DataGridView.网格不会自动生成列.我已手动设置列名称和属性.

这包括单元格的DefaultCellStyle.我希望所有类型为DataGridViewCheckBoxColumn的单元格都居中对齐.我已在设计器中为此类型的每个列手动设置此对齐方式.加载datagridview时,这些更改不会显示.

作为我的下一个方法,我在DataBindingComplete事件中使用了下面的代码(也没有任何效果)

foreach (DataGridViewColumn dgvc in this.dgv_Automations.Columns)
{
    if(dgvc.CellType == typeof(DataGridViewCheckBoxCell))
        dgvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
Run Code Online (Sandbox Code Playgroud)

调试时,上面的代码已经将对齐设置为中心.但是,它仍然没有显示在网格上.

CAN得到它DataBindingComplete事件中,我做这样的事情在工作,如果:

foreach (DataGridViewRow dgvr in this.dgv_Automations.Rows)
{
    dgvr.Cells["isErrors"].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
Run Code Online (Sandbox Code Playgroud)

但是,我有很多这样的列,这样做真的没有意义.编辑:我已经用这种方式实现了它,只有大约10行,在每个单元格上设置此属性有非常明显的延迟.

我的问题是:这样做的正确方法是什么?为什么设计师DefaultCellStyle不起作用?

Ste*_*eve 7

根据MSDN所述DefaultCellStyleDataGridViewColumn是通过将指定的样式覆盖DataGridView.RowsDefaultCellStyle,DataGridView.AlternatingRowsDefaultCellStyle,DataGridViewRow.DefaultCellStyle,和DataGridViewCell.Style特性.
因此,可能会将其中一种样式设置为与您所需的对齐方式不兼容的值(例如DataGridViewContentAlignment.NotSetDataGridViewContentAlignment.TopLeft)