DataGridView单元格对齐将无法正常工作 [英] DataGridView Cell Alignment Won't Work
问题描述
这包括单元格的DefaultCellStyle。我想要所有类型为DataGridViewCheckBoxColumn的单元格对齐中心。我已经在设计器中为此类型的每一列手动设置了此对齐方式。加载datagridview时,这些更改不会显示。
作为我的下一个方法,我使用下面的代码在DataBindingComplete事件(也没有影响) p>
foreach(this.dgv_Automations.Columns中的DataGridViewColumn dgvc)
{
if(dgvc.CellType == typeof (DataGridViewCheckBoxCell))
dgvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
调试时,上述代码已将对齐方式设置为中心。但是,它仍然没有显示在网格上。
我可以使其工作,如果在DataBindingComplete事件期间我做这样的事情:
foreach(this.dgv_Automations.Rows中的DataGridViewRow dgvr)
{
dgvr.Cells [isErrors ] .Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
但是,我有很多列,真的没有意义这样做。编辑:我已经实现了这个方式,只有大约10行,在每个单元格上设置这个属性是非常显着的滞后。
我的问题是:什么是正确的这样做的方式?为什么设计师DefaultCellStyle不起作用?
根据 MSDN <$ c $ DefaultCellStyle
$ c> DataGridViewColumn 被通过 DataGridView.RowsDefaultCellStyle
, DataGridView.AlternatingRowsDefaultCellStyle $ c $指定的样式覆盖c>,
DataGridViewRow.DefaultCellStyle
和 DataGridViewCell.Style
属性。
所以可能这些样式之一设置为与您所需的对齐方式不兼容的值(例如 DataGridViewContentAlignment.NotSet
或 DataGridViewContentAlignment.TopLeft
)
I have a DataGridView that I use in a C# Winforms project. The grid does NOT autogenerate columns. I have manually set the column names and properties.
This includes the DefaultCellStyle for the cells. I want all cells that are of type DataGridViewCheckBoxColumn to be aligned centered. I have manually set this alignment for each column of this type in the designer. These changes do not show up when the datagridview is loaded.
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 had the alignment set the center. However, it still does not show up on the grid.
I CAN get it working 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 many of these columns and it really doesn't make sense to do it this way. EDIT: I have implemented it this way and with only about 10 rows there is very noticeable lag from setting this property on each cell.
My question is: what is the proper way of doing this? Why does the designer DefaultCellStyle not work?
According to MSDN the DefaultCellStyle
of a DataGridViewColumn
is overridden by the styles specified through the DataGridView.RowsDefaultCellStyle
, DataGridView.AlternatingRowsDefaultCellStyle
, DataGridViewRow.DefaultCellStyle
, and DataGridViewCell.Style
properties.
So it's possibile that one of these styles are set to a value incompatible with your required alignment (for example DataGridViewContentAlignment.NotSet
or DataGridViewContentAlignment.TopLeft
)
这篇关于DataGridView单元格对齐将无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!