Alexus
Alexus

Reputation: 296

DataGrid strange behaviour for autogenerated columns order

I use a datagrid binded to an observable collection in my project, but I have an issue with autogenerated columns order.

Everything was ok before I added a new property to the binded class, now the Name property is generated in third column...

This is the class :

[StructLayout(LayoutKind.Sequential)]
[XmlRoot("ConfData")]
public class XapDataVals : XapBaseClass, INotifyPropertyChanged
{
    [XmlAttribute]
    public string Name { get; set; } = "";

    [XmlAttribute]
    public float Min { get; set; } = 0;

    [XmlAttribute]
    public float Max { get; set; } = 0;

    [XmlAttribute]
    public float ALVal { get; set; } = 0;

    /* more properties */

    public event PropertyChangedEventHandler PropertyChanged;
    protected void PropertyChangedNotify(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public XapDataVals()
    {

    }
}

There is nothing special about order in Datagrid's AutoGeneratinColumn event function, only style applying.

The XAML code for datagrid :

<DataGrid x:Name="DgDataTable" Grid.Row="0" Grid.ColumnSpan="2" Margin="10,10,10,0" FrozenColumnCount="1" AutoGenerateColumns="true" HeadersVisibility="All" RowHeaderWidth="20" GridLinesVisibility="Horizontal"
              Style="{StaticResource AzureDataGrid}"  LoadingRow="dgDataTable_LoadingRow" AlternatingRowBackground="{DynamicResource {x:Static SystemColors.GradientActiveCaptionBrushKey}}" 
              AutoGeneratingColumn="DgDataTable_AutoGeneratingColumn" CellStyle="{StaticResource CommonCell}" CurrentCellChanged="DgDataTable_CurrentCellChanged" PreviewMouseLeftButtonDown="DgDataTable_PreviewMouseLeftButtonDown" 
              Drop="DgDataTable_Drop" AllowDrop="True" IsManipulationEnabled="True" PreviewMouseRightButtonDown="DgDataTable_PreviewMouseRightButtonDown" CanUserSortColumns="False">
        <DataGrid.RowHeaderStyle>
            <Style TargetType="DataGridRowHeader">
                <Setter Property="FontSize" Value="10"/>
                <Setter Property="Background" Value="LightCyan"/>
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
            </Style>
        </DataGrid.RowHeaderStyle>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="BorderBrush" Value="Blue" />
                        <Setter Property="BorderThickness" Value="1" />
                    </Trigger>
                </Style.Triggers>
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Blue" />
                </Style.Resources>
            </Style>
        </DataGrid.RowStyle>
        <DataGrid.RowValidationRules>
            <DataErrorValidationRule ValidatesOnTargetUpdated="True" ValidationStep="UpdatedValue" />
        </DataGrid.RowValidationRules>
    </DataGrid>

And the generated grid order :

enter image description here

What could I change to make Name column be displayed in position first again ?

Thank you for your help.

Upvotes: 0

Views: 376

Answers (1)

Tony_KiloPapaMikeGolf
Tony_KiloPapaMikeGolf

Reputation: 889

Please read into the DataGrid.AutoGeneratedColumns Event

<sdk:DataGrid x:Name="yourcolorfulDatagrid"
        AutoGenerateColumns="True"
        AutoGeneratingColumn="yourcolorfulDatagrid_AutoGeneratingColumn"/>

And then the event:

private void yourcolorfulDatagrid_AutoGeneratedColumns(object sender, EventArgs e)
{
        yourcolorfulDatagrid_.Columns.FirstOrDefault(x => x.Header.ToString() == "Name").DisplayIndex = 0;
}

Upvotes: 1

Related Questions