DataGridView的单元格对齐方式无法工作 [英] DataGridView Cell Alignment Won't Work

查看:863
本文介绍了DataGridView的单元格对齐方式无法工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这包括DefaultCellStyle的细胞。我想这是类型DataGridViewCheckBoxColumn的对齐所有的细胞中心。我已经手动设置这种对齐对于这种类型在设计的每一列。被装载在DataGridView时,这些变化不会显示出来。

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.

由于我的下一个方法,我用在DataBindingComplete事件下面的代码(也没有效果)

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.

我CAN 搞不定如果DataBindingComplete活动期间我做这样的事情:

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;
}



不过,我有很多这些列,它确实没有意义做这种方式。编辑:我已经实现这种方式,用大约只有10行有一个从设置在每个单元上这个属性很明显的滞后

However, I have many of these columns and it really doesn't make sense to do it this way. I have implemented it this way and with only about 10 rows there is very noticeable lag from setting this property on each cell.

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

My question is: what is the proper way of doing this? Why does the designer DefaultCellStyle not work?

推荐答案

据的 MSDN 中的 DefaultCellStyle C>的DataGridViewColumn 是通过 DataGridView.RowsDefaultCellStyle DataGridView.AlternatingRowsDefaultCellStyle , DataGridViewRow.DefaultCellStyle DataGridViewCell.Style 属性。结果
所以这是不可能性,一个这些样式设置为与您所需的对齐方式不兼容的一个值(例如 DataGridViewContentAlignment.NotSet DataGridViewContentAlignment.TopLeft

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆