vb.net DataGridView 标题对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2101207/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:37:28  来源:igfitidea点击:

DataGridView header alignment

vb.netwinformsdatagridview

提问by somu

I am using vb.net 2005. I want one clarification for datagridview.

我正在使用 vb.net 2005。我想要一个关于 datagridview 的说明。

I use the following property to set the alignment of header text:

我使用以下属性来设置标题文本的对齐方式:

DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter

This property applies to all header cells. I only want to change one headers alignment property.

此属性适用于所有标题单元格。我只想更改一个标题对齐属性。

How can this be done?

如何才能做到这一点?

回答by David Hall

To change just the one header cell you need to select the column you want from the DataGridView columns collection.

要仅更改一个标题单元格,您需要从 DataGridView 列集合中选择所需的列。

dataGridView1.Columns(0).HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter;

dataGridView1.Columns("ColumnName").HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter;

I've shown two examples - one using the column index in the columns collection, the other using the name given to the column.

我已经展示了两个示例 - 一个使用列集合中的列索引,另一个使用为列指定的名称。

Each column in the DataGridView then has a HeaderCell property, with a Style.Alignment that can be set with a value from the DataGridViewContentAlignment enum.

DataGridView 中的每一列都有一个 HeaderCell 属性,带有一个 Style.Alignment,可以使用 DataGridViewContentAlignment 枚举中的值进行设置。

One additional thing to note is that these alignments are affected by the sorting glyph in the column. If the alignment does not appear how you expect, the code below can sometimes resolve the issue by removing the sorting glyph.

需要注意的另一件事是这些对齐方式受列中排序字形的影响。如果对齐方式不符合您的预期,下面的代码有时可以通过删除排序字形来解决问题。

dataGridView1.Columns(0).SortMode = DataGridViewColumnSortMode.NotSortable;

回答by Maxence

There is a property HeaderCellon the column object which allow you to access the style:

HeaderCell列对象上有一个属性可让您访问样式:

dataGridView1.Columns(0).HeaderCell.Style.Alignment 
  = DataGridViewContentAlignment.BottomCenter

回答by Matt

For column headersyou can do this

对于列标题,您可以执行此操作

DataGrid d = new DataGrid();
d.Columns[0].HeaderStyle.VerticalAlign = VerticalAlign.Bottom;

For cell contentyou can do this

对于单元格内容,您可以执行此操作

DataGrid d = new DataGrid();
d.Columns[0].ItemStyle.VerticalAlign = VerticalAlign.Bottom;

EDIT: Just realised that you were asking about GridViews - This will be the same for that too

编辑:刚刚意识到您在询问 GridViews - 这也是一样的

eg
GridView gv = new GridView ();
gv.Columns[0].ItemStyle.VerticalAlign = VerticalAlign.Bottom;

Hope this helps

希望这可以帮助

回答by somu

datagridview.Columns(linti).HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter

datagridview.Columns(linti).HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter

here linti means columnno or id

这里 linti 表示 columnno 或 id

回答by Gabriel McAdams

Change

改变

DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter

to

DataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;