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

问题描述

我有一个奇怪的问题,这可能是一个简单的修复,但经过多次研究,我似乎找不到解决方案。



我有一个 DataGridView ,我试图将列标题放在中心位置,但是结果是偏心的偏心 - 几乎像一个缩进的问题。我在一个或两个网站上看到过这个问题的一些帖子,但从来没有一个解决方案。有什么想法吗?



以下是我目前使用的声明:

  DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter 


解决方案

代码您已经发布在正确的轨道上:您需要设置 DataGridView 控件的 ColumnHeadersDefaultCellStyle 属性。 p>

但是,您需要创建一个新的 DataGridViewCellStyle 类,并将分配给 ColumnHeadersDefaultCellStyle 属性。您不能修改 Alignment 属性,因为您的代码示例显示,除非您有为此属性分配了一个 DataGridViewCellStyle 类。



所以,例如,以下代码实现了完美的中心列标题一个空白的项目:

  Dim dgvColumnHeaderStyle作为新的DataGridViewCellStyle()
dgv ColumnHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

myDataGridView.ColumnHeadersDefaultCellStyle = dgvColumnHeaderStyle

;





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






编辑:我刚刚注意到您在列中指出的轻微偏移 - 确实创建了一个每个标题右侧有一点额外的填充。这不是一个bug。有一个更简单的解释。



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



如果您希望列标题为完全,您需要禁用排序。将列的 SortMode 属性设置为NonSortable。每当列文本为中心或右对齐时,这应该可以防止为排序字形保留空格。


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?

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.

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


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.


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.

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的列中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 13:38