[SOLVED] WPF Datagrid showing extra column


My grid is showing an extra column.
Can someone help me please? I have two columns. Document name and document date. What is the third column that is showing up? The datasource has four properties. I need 2 of them.

Also, When the grid first loads how do I highlight the first row

enter image description here

 <Grid   VerticalAlignment="Stretch" Focusable="True">
        <ColumnDefinition Width="500" ></ColumnDefinition>
        <ColumnDefinition Width="6"></ColumnDefinition>
        <ColumnDefinition Width="*" />
    <GridSplitter Grid.Column="1" Grid.RowSpan="1" HorizontalAlignment="Center" VerticalAlignment="Stretch"  
                  BorderBrush="DarkSlateGray" BorderThickness="1" Width="6" ShowsPreview="True"  >
            <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                <GradientStop Color="#FF808385" Offset="0"/>
                <GradientStop Color="#FFECF1F7" Offset="1"/>
    <ScrollViewer Name="scrollViewer" PreviewMouseWheel="ScrollViewer_OnPreviewMouseWheel">
        <DataGrid Name = "dataGrid"   
                  SelectionChanged="ShowDocument"  AutoGenerateColumns="False"
                  PreviewMouseWheel="DataGrid_PreviewMouseWheel"   >


                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding Name}" />
                                            <ItemsPresenter />
                <DataGridTextColumn    Header = "Document Name" Binding = "{Binding Name, Mode=OneWay}" SortMemberPath="Name" />
                <DataGridTextColumn Header = "Document Date" Binding = "{Binding DocumentDate, StringFormat=\{0:d\}, Mode=OneWay}" SortMemberPath="DocumentDate" />



Seems like you need to allow second column in your grid to use the remaining space like so:

<DataGridTextColumn Binding="{Binding DocumentDate, StringFormat=\{0:d\}, Mode=OneWay}"
                    Header="Document Date"
                    SortMemberPath="DocumentDate" />

See also: https://stackoverflow.com/a/5028932/7127128

Also you can select the first line by setting selected index in DataGrid:

<DataGrid Name = "dataGrid"

Answered By – plori

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *