-1

I have a simple C# program with some data in a DataTable, which is bound to a WPF DataGrid.

<DataGrid Grid.Row="1" DataContext="{Binding}" Name="dataGridEditTab"

and

dataGridEditTab.ItemsSource = NavTable.DefaultView;

I have an "Add Row" button that adds a blank row to the DataTable when pressed. The blank row shows up on the DataGrid fine, but it is not selected. I want to automatically select the new row, but I cannot figure out how to do this. I think I need to set SelectedItem to something, but I haven't figured out what. Any help? Thanks.

user584974
  • 157
  • 2
  • 6

2 Answers2

0

According to this thread (How to set selected row of DataGridView to newly-added row when the grid is bound to sorted DataView) you can accomplish this using the DataGridViewRowsAddedEventArgs. Basically create a global variable to place the newly added row index in. Assign the appropriate value to that index in the EventArgs, and then use that index to select the row you are looking for.

Community
  • 1
  • 1
NealR
  • 10,189
  • 61
  • 159
  • 299
0

In case it is MVVM use binding for SelectedItem or SelectedIndex. If not, bettter to use it)

To achieve your goal just bind SelectedIndex and whenever you add the row, set SelectedIndex as RowsCount-1;

Here is an example.

<Grid >

    <DataGrid AutoGenerateColumns="False" CanUserAddRows="False" ItemsSource="{Binding Path=Persons}" Margin="0,65,0,0" 
                  SelectedIndex="{Binding Path=SelectedIndex, Mode=TwoWay}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="First name" Binding="{Binding Path=FirstName}" Width="*"/>
            <DataGridTextColumn Header="Last name" Binding="{Binding Path=LastName}" Width="*"/>
        </DataGrid.Columns>
    </DataGrid>
    <Button Content="Add new row" Height="23" HorizontalAlignment="Left" Margin="317,12,0,0" Command="{Binding Path=AddCommand}" VerticalAlignment="Top" Width="112" />
</Grid>

and viewmodel

public class MainViewModel : INotifyPropertyChanged
{
    private int _selectedPerson;

    public MainViewModel()
    {
        AddCommand = new RelayCommand(AddAndSelectePerson);
        Persons = new DataTable();
        Persons.Columns.Add("FirstName");
        Persons.Columns.Add("LastName");
        Persons.Rows.Add("Alexandr", "Puskin");
        Persons.Rows.Add("Lev", "Tolstoy");
    }

    public ICommand AddCommand { get; private set; }

    public int SelectedIndex
    {
        get { return _selectedPerson; }
        set
        {
            _selectedPerson = value;
            OnPropertyChanged("SelectedIndex");
        }
    }

    public DataTable Persons { get; private set; }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    private void AddAndSelectePerson()
    {
        Persons.Rows.Add();
        SelectedIndex = Persons.Rows.Count - 1;
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class RelayCommand : ICommand
{
    private readonly Action _actionToExecute;

    public RelayCommand(Action actionToExecute)
    {
        _actionToExecute = actionToExecute;
    }

    #region ICommand Members

    public void Execute(object parameter)
    {
        _actionToExecute();
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    #endregion
}

public class Person
{
    public Person()
    {
    }

    public Person(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }

    public string FirstName { get; set; }

    public string LastName { get; set; }
}

Example to try

Artiom
  • 7,694
  • 3
  • 38
  • 45