C#, WPF Desktop DataGrid select first row.

Markus Freitag 3,786 Reputation points
2020-08-04T13:14:59.92+00:00

Hello,

   <DataGrid Grid.Column="0"        
                ItemsSource="{Binding View1}" 
                  IsReadOnly="True"
                  AutoGenerateColumns="False"  FontSize="18" SelectionMode="Single" SelectionUnit="FullRow" >
                <DataGrid.Resources>

The goal is to always have the first line active.
If there is a change in the list, the first line should be selected automatically via Code behind or the right settings (XAML) here.
I can select another line to see what the content is.
If something changes, select the first line again.

The first line is the active one.

How can I do that?

FirstIn, FirstOut

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
0 comments No comments
{count} votes

Accepted answer
  1. Markus Freitag 3,786 Reputation points
    2020-08-05T05:53:37.483+00:00

    Hello Peter,
    Almost right. Looks good too.

    I have an order list. I'll fill it out.
    Now the operator can select another order to see what it contains. He can't make any changes.

    The order list is processed automatically.
    Target quantity and current quantity. (code behind, the list is only read only)

    The idea is then to select the first line again when making this change, because that is the active order.

    Application.Current.Dispatcher.Invoke(() => { CViewSource1.View.MoveCurrentToFirst(); });

    Works with it.

    1-> I select a other row.
    2 -> The code behind calls MoveCurrentToFirst --> Works too

    How can I display the line in blue? Then the operator can see it better!
    How can I simply assign a different color?
    15628--yes-1.png

    I think if i call this MoveCurrentToFirst(), the next should be to call something like this setFocus
    How can I do it?


1 additional answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,326 Reputation points
    2020-08-04T19:20:20.263+00:00

    Hi Markus,
    use MoveCurrentToFirst. Try demo:

    <Window x:Class="WpfApp1.Window67"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp67"  
            mc:Ignorable="d"  
            Title="Window67" Height="450" Width="800">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
      <Grid>  
        <DataGrid ItemsSource="{Binding View}"/>  
      </Grid>  
    </Window>  
    

    And Code:

    15519-x.png
    15611-04-08-2020-21-19-15.gif

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.