0

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

            <DataGrid.GroupStyle>

                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <StackPanel>
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding Name}" />
                                               
                                            </StackPanel>
                                            <ItemsPresenter />
                                        </StackPanel>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </DataGrid.GroupStyle>
            
            <DataGrid.Columns>
                
                <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" />
                
            </DataGrid.Columns>
        </DataGrid>


   </ScrollViewer>
user575219
  • 2,346
  • 15
  • 54
  • 105
  • 1
    You have 3 ColumnDefinitions. The 3rd one gets the remaining space. Use just two ColumnDefinitions? – Stephan Schlecht Mar 15 '22 at 06:34
  • 1
    Does this answer your question? [How do I make XAML DataGridColumns fill the entire DataGrid?](https://stackoverflow.com/questions/5028894/how-do-i-make-xaml-datagridcolumns-fill-the-entire-datagrid) – Orace Mar 15 '22 at 09:35

1 Answers1

2

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"
                    Width="*"
                    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"
          ...
          SelectedIndex="0"
          ...>
plori
  • 331
  • 3
  • 7
  • Thank you very much for your help. I do have one thing regarding selected index for the datagrid. I need the first row highlighted to indicate that a row has been selected when the grid loads. There are so many ways on google. – user575219 Mar 15 '22 at 11:26
  • @When the grid loads no row is highlighted. When I click on a row, it is then highlighted. I click it again its not highlighted again. There should always be a highlighted row in the datagrid as long as it has data. Its is a nice to have itme – user575219 Mar 15 '22 at 11:45