[英]How can I center the heading in a column on a DataGridView?
I have a strange problem and it's probably a simple fix, but after much research, I cannot seem to find a solution. 我有一个奇怪的问题,它可能是一个简单的解决方法,但是经过大量研究,我似乎找不到解决方案。
I have a DataGridView
on which I'm trying to center the column headings, but the result is a left bias in the centering—almost like an indenting problem. 我有一个
DataGridView
,试图将列标题居中,但是结果是居中出现左偏-几乎像一个缩进问题。 I've seen a few posts on this issue on a site or two, but never a solution. 我在一两个网站上看到过一些关于此问题的文章,但从未找到解决方案。 Any thoughts?
有什么想法吗?
Here's the statement I'm currently trying to use: 这是我当前正在尝试使用的语句:
DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
The code you've posted is on the right track: you need to set the ColumnHeadersDefaultCellStyle
property of your DataGridView
control. 您发布的代码在正确的轨道上:您需要设置
DataGridView
控件的ColumnHeadersDefaultCellStyle
属性。
However, you need to create a new DataGridViewCellStyle
class and assign that to the ColumnHeadersDefaultCellStyle
property. 然而,你需要创建一个新
DataGridViewCellStyle
类并分配到ColumnHeadersDefaultCellStyle
财产。 You can't modify the Alignment
property as your code sample shows unless you have assigned a DataGridViewCellStyle
class to this property. 除非已为该属性分配
DataGridViewCellStyle
类,否则无法修改代码示例所显示的Alignment
属性。
So, for example, the following code achieves perfectly centered column headings in a blank project: 因此,例如,以下代码在空白项目中实现了居中对齐的列标题:
Dim dgvColumnHeaderStyle As New DataGridViewCellStyle()
dgvColumnHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
myDataGridView.ColumnHeadersDefaultCellStyle = dgvColumnHeaderStyle
In the future, you may find it easier to do these types of things from the Designer. 将来,您可能会发现从Designer进行这些类型的操作会更容易。 If you still need to do it yourself through code, you can check the
*.Designer.vb
file that is created to see how it was done. 如果仍然需要自己通过代码进行操作,则可以检查所创建的
*.Designer.vb
文件,以了解其完成方式。
EDIT: I just now noticed the slight offset you're referring to in the columns—it does indeed create a little extra padding to the right of each header. 编辑:我刚才注意到您在列中所指的轻微偏移量—实际上确实在每个标题的右侧创建了一些额外的填充。 It's not a bug, though.
不过,这不是错误。 There's a much simpler explanation.
有一个简单得多的解释。
Like a ListView
, the DataGridView
supports sorting by columns. 像
ListView
一样, DataGridView
支持按列排序。 Therefore, each column header reserves enough space to display the sort glyph (usually an arrow) when calculating center justification. 因此,在计算中心对齐时,每个列标题都保留了足够的空间来显示排序标志符号(通常是箭头)。
If you want the column headers to be perfectly centered, you'll need to disable sorting. 如果要使列标题完美居中,则需要禁用排序。 Set the
SortMode
property for the column to "NonSortable". 将列的
SortMode
属性设置为“ NonSortable”。 This should prevent space from being reserved for the sort glyph whenever the column text is center or right justified. 每当列文本居中或右对齐时,这都应防止为排序字形保留空间。
如果要居中或使用“列标题”文本的任何其他对齐方式,则可以使用此
dgvResults.Columns("ColumnName").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.