vb.net 如何在 DataGridView 的列中居中标题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4550836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I center the heading in a column on a DataGridView?
提问by Keith
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. I've seen a few posts on this issue on a site or two, but never a solution. Any thoughts?
我有一个DataGridView
我试图将列标题居中,但结果是居中出现左偏差 - 几乎就像一个缩进问题。我在一两个网站上看到了一些关于这个问题的帖子,但从来没有一个解决方案。有什么想法吗?
Here's the statement I'm currently trying to use:
这是我目前尝试使用的语句:
DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
回答by Cody Gray
The code you've posted is on the right track: you need to set the ColumnHeadersDefaultCellStyle
property of your DataGridView
control.
您发布的代码走在正确的轨道上:您需要设置控件的ColumnHeadersDefaultCellStyle
属性DataGridView
。
However, you need to create a new DataGridViewCellStyle
class and assign thatto the ColumnHeadersDefaultCellStyle
property.You can't modify the Alignment
property as your code sample shows unless you have assigned a DataGridViewCellStyle
class to this property.
然而,你需要创建一个新的DataGridViewCellStyle
类,并分配是对ColumnHeadersDefaultCellStyle
财产。Alignment
除非您已DataGridViewCellStyle
为该属性分配了一个类,否则您无法像代码示例所示那样修改该属性。
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. 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 执行这些类型的事情会更容易。如果您仍然需要通过代码自己完成,您可以检查*.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. Therefore, each column header reserves enough space to display the sort glyph (usually an arrow) when calculating center justification.
像 a 一样ListView
,DataGridView
支持按列排序。因此,在计算居中对齐时,每个列标题都会保留足够的空间来显示排序字形(通常是箭头)。
If you want the column headers to be perfectlycentered, you'll need to disable sorting.Set the SortMode
property for the column to "NonSortable". This should prevent space from being reserved for the sort glyph whenever the column text is center or right justified.
如果您希望列标题完全居中,则需要禁用排序。将SortMode
列的属性设置为“NonSortable”。这应该可以防止在列文本居中或右对齐时为排序字形保留空间。
回答by mikro
If you want to center or use any other alignment style of the Column Header text you can use this
如果要居中或使用列标题文本的任何其他对齐样式,可以使用此
dgvResults.Columns("ColumnName").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter