donL
donL

Reputation: 1300

How to change Autogeneratedcolumn text alignment

I am trying to figure out how to change the text alignment of an auto generated column in code.

    Private Sub dgBook_AutoGeneratingColumn(sender As System.Object, e As System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs) Handles dgBook.AutoGeneratingColumn
        If e.PropertyType = GetType(DateTime) Then
            Dim dataGridTextColumn As DataGridTextColumn = TryCast(e.Column, DataGridTextColumn)
            If dataGridTextColumn IsNot Nothing Then
                dataGridTextColumn.Binding.StringFormat = "{0:d}"
            End If
        End If

        If e.PropertyName = "Amount" Then
            Dim dataGridTextColumn As DataGridTextColumn = TryCast(e.Column, DataGridTextColumn)
            If dataGridTextColumn IsNot Nothing Then
                dataGridTextColumn.Binding.StringFormat = "{0:#,##0.00;(#,##0.00)}"
                'I tried the next line for testing but it did not work
                dataGridTextColumn.SetValue(TextBox.TextAlignmentProperty, TextAlignment.Center)
            End If
        End If
     End Sub

Upvotes: 4

Views: 2427

Answers (4)

Linus Proxy
Linus Proxy

Reputation: 593

Haven't found a static oneliner because there's a void involved, but this is what I do:

public partial class MainWindow : Window
{
    private static readonly Style someColumnStyle = new(typeof(DataGridCell));
    private static readonly Setter rightAlignSetter = new(HorizontalAlignmentProperty, HorizontalAlignment.Right);

    public MainWindow()
    {
        InitializeComponent();
        someColumnStyle.Setters.Add(rightAlignSetter);
        /* add more styles as needed here, i.e. FontFamily etc */
    }

    private void GridSourceSettings_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        string header = e.Column.Header.ToString();
        if (header == "SomePropertyName")
            e.Column.CellStyle = someColumnStyle;
    }
}

Upvotes: 0

NatCh
NatCh

Reputation: 48

I've been trying to do this myself for a couple of days, but I needed/wanted a solution that was purely code behind. Through a lot of trial and error, I finally found several different pieces in various places that I could put together to arrange the alignment the way I wanted it.

I know this is C# and the original question is VB aimed, but perhaps it will parse over well enough.

Here's what worked for me so the next person to come along won't have to work quite so hard:

First I set up a style to right align the contents of a column on the WPF DataGrid

Style rightStyle = new Style { TargetType = typeof(DataGridCell) };
style.Setters.Add(new Setter(Control.HorizontalAlignmentProperty, HorizontalAlignment.Right));

Then I set the CellStyle to the defined style on the columns I wanted right aligned,

dataGridTextColumn.CellStyle = style;

Just like everything else, it's simple ... once you know which stinkin' simple thing(s) you're after.

And of course this could be adjusted to align center or left, etc.

I don't mean to take credit for this, I only took code others had posted and reassembled the various pieces. I just finally have some small contribution to offer to the site which has been tremendously helpful to me in my own efforts to figure out this .NET stuff, so I want to offer it.

Upvotes: 0

donL
donL

Reputation: 1300

So what I ended up doing was setting up a style in the XAML WPF code

<Window.Resources>
    <Style TargetType="DataGridCell" x:Key="rightAlignCell">
        <Setter Property="HorizontalAlignment" Value="Right"></Setter>
    </Style>
</Window.Resources>

And then I set the cell style to that style in the code behind.

Private Sub datagrid1_AutoGeneratingColumn(sender As System.Object, e As System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs) Handles datagrid1.AutoGeneratingColumn
    If e.PropertyName = "Amount" Then
        Dim dataGridTextColumn As DataGridTextColumn = TryCast(e.Column, DataGridTextColumn)
        If dataGridTextColumn IsNot Nothing Then
            dataGridTextColumn.Binding.StringFormat = "{0:#,##0.00;(#,##0.00)}"
        End If
        e.Column.CellStyle = TryCast(FindResource("rightAlignCell"), Style)
    End If
end sub

Upvotes: 2

atomaras
atomaras

Reputation: 2568

See here DataGridTextColumn Text Alignment

Upvotes: 1

Related Questions