Dot NET
Dot NET

Reputation: 4907

DataGrid column headers not aligned with data

I've got a DataGrid, which is quite simple as far as DataGrids go. For some reason or other, the headers are not aligned with the rest of the data, as shown in the screenshot below:

enter image description here

I've searched the internet but cannot seem to find a solution for it. Here is my DataGrid code:

Grid>
        <DataGrid Name="dgAttributes" 
                  ItemsSource="{Binding itemsSource}" 
                  AutoGenerateColumns="False" 
                  CanUserAddRows="False" 
                  CanUserDeleteRows="False" 
                  CanUserReorderColumns="False" 
                  CanUserResizeColumns="False" 
                  CanUserResizeRows="False"
                  CanUserSortColumns="False"
                  >
            <DataGrid.Columns>
                <DataGridTextColumn Width="Auto" IsReadOnly="True" Binding="{Binding Field}" Header="Fields"/>
                <DataGridComboBoxColumn  Width="95" IsReadOnly="False" Header="Order" ItemsSource="{Binding Source={StaticResource SortOrderProvider}}" SelectedItemBinding="{Binding SortBy, Mode=TwoWay}"/>
                <DataGridCheckBoxColumn Width="Auto" IsReadOnly="False" Binding="{Binding GroupBy}" Header="Group By"/>
                <DataGridComboBoxColumn Width="85" IsReadOnly="False" Header="Aggregate" ItemsSource="{Binding Source={StaticResource AggregateProvider}}" SelectedItemBinding="{Binding AggregateBy, Mode=TwoWay}"/>
                <DataGridTextColumn Width="Auto" IsReadOnly="False" Binding="{Binding Having}" Header="Having"/>
                <DataGridTextColumn Width="Auto" IsReadOnly="False" Binding="{Binding DisplayOrder}" Header="Display Order"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>

It may also be worth mentioning that when I click on one of the Combobox cells, the headers align themselves properly.

Upvotes: 5

Views: 3862

Answers (4)

Ashi
Ashi

Reputation: 864

Finally I defeated this problem Find the solution here.

Sorry I did not notice you are not overriding the DataGrid control template. I am afraid , you will have to define a control template in order to correct the DataGrid behavior.

PS : I have .NET Framework 4.0

Upvotes: 2

Atomic Star
Atomic Star

Reputation: 5517

I found that simply setting the HeadersVisiblity to "Column" does the trick - see XAML below.
Easier than getting hold of that SelectAll Button...

<DataGrid x:Name="myGrid" HeadersVisibility="Column">

Upvotes: 2

Vinit Sankhe
Vinit Sankhe

Reputation: 19895

You are definitely having some style or something that is hiding the top left Select All button of datagrid. Hence the columns are shifted to left a little.

Use this thread to get hold of that button in DataGrid.OnLoad and check its Visibility property.

Select All button WPF DataGrid

If its collpased/hidden, set the visibility to Visbility.Visible. Or check its Width being zero and set appropriate Width.

Upvotes: 3

Vikas
Vikas

Reputation: 376

You can Use the Property of the DataGrid Like and also define the particular css class for the Row

HeaderStyle-HorizontalAlign=""
HeaderStyle-CssClass=""

Upvotes: 0

Related Questions