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. 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.
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>
<GridViewColumn>
<GridViewColumnHeader
HorizontalContentAlignment="Center"
TextBlock.TextAlignment="Center" Content="Some String"/>
I assume something similar would work for DataGridView.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.