简体   繁体   English

C# DataGridview ColumnHeader Multi Line Text Center or Right Alignment

[英]C# DataGridview ColumnHeader Multi Line Text Center or Right Alignment

I am facing a problem in C# Datagridview that I have multi line text in the ColumnHeaders of that Datagridview and I want the text to be centered in both lines.我在 C# Datagridview 中遇到一个问题,我在该 Datagridview 的 ColumnHeaders 中有多行文本,我希望文本在两行中居中。 Currently, its showing centered text only when there is single line, but in case of multiline, it behaves like this:目前,它仅在单行时显示居中文本,但在多行的情况下,它的行为如下:

|L1        |
|L2 Text   | 

And I want it to be appeared as我希望它显示为

|    L1    |
| L2 Text. |

I tried properties我试过属性

DG.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
DG.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

I don't have much points to upload image or screenshot, so sorry for that.我没有太多要上传图片或屏幕截图的要点,对此深表歉意。 Any help would be appreciated, thanks in advance.任何帮助将不胜感激,在此先感谢。

I got it finally. 我终于明白了 This piece of code worked for me. 这段代码对我有用。

dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;
dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

Thanks anyways. 不管怎么说,多谢拉。

Try this.. 尝试这个..

In your gridview load method. 在您的gridview加载方法中。

foreach(DataGridViewColumn col in dgvYourGridName.Columns)
{
    col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

}

I know for a GridView you can add HorizontalContentAlignment="Center" and TextBlock.TextAlignment="Center" to the GridViewColumnHeader tag in the xaml.我知道对于 GridView,您可以将 HorizontalContentAlignment="Center" 和 TextBlock.TextAlignment="Center" 添加到 xaml 中的 GridViewColumnHeader 标记。

<GridView>
    <GridViewColumn>
        <GridViewColumnHeader 
            HorizontalContentAlignment="Center"
            TextBlock.TextAlignment="Center" Content="Some String"/>

I assume something similar would work for DataGridView.我假设类似的东西适用于 DataGridView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM