如何在 DataGridView 的列中居中标题? [英] How can I center the heading in a column on a DataGridView?

查看:83
本文介绍了如何在 DataGridView 的列中居中标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,它可能是一个简单的解决方法,但经过大量研究,我似乎找不到解决方案.

I have a strange problem and it's probably a simple fix, but after much research, I cannot seem to find a solution.

我有一个 DataGridView,我试图在其上居中列标题,但结果是居中出现左偏——几乎就像一个缩进问题.我在一两个网站上看到了一些关于这个问题的帖子,但从来没有一个解决方案.有什么想法吗?

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?

这是我目前尝试使用的语句:

Here's the statement I'm currently trying to use:

DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

推荐答案

您发布的代码走在正确的轨道上:您需要设置 DataGridViewColumnHeadersDefaultCellStyle 属性/code> 控制.

The code you've posted is on the right track: you need to set the ColumnHeadersDefaultCellStyle property of your DataGridView control.

但是,您需要创建一个新的 DataGridViewCellStyle 类并将那个分配给 ColumnHeadersDefaultCellStyle 属性.除非您已将 DataGridViewCellStyle 类分配给此属性,否则无法像代码示例所示那样修改 Alignment 属性.

However, you need to create a new DataGridViewCellStyle class and assign that to 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.

因此,例如,以下代码在空白项目中实现了完全居中的列标题:

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

 


将来,您可能会发现从 Designer 执行这些类型的事情会更容易.如果您仍然需要通过代码自己完成,您可以查看创建的 *.Designer.vb 文件以查看它是如何完成的.


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.

我刚刚注意到您在列中提到的轻微偏移 - 它确实在每个标题的右侧创建了一些额外的填充.不过,这不是一个错误.有一个更简单的解释.

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.

ListView 一样,DataGridView 支持按列排序.因此,在计算居中对齐时,每个列标题都会保留足够的空间来显示排序字形(通常是箭头).

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.

如果您希望列标题完美居中,您需要禁用排序.设置列的 SortMode 属性到不可排序".这应该可以防止在列文本居中或右对齐时为排序字​​形保留空间.

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". This should prevent space from being reserved for the sort glyph whenever the column text is center or right justified.

这篇关于如何在 DataGridView 的列中居中标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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