将新行添加到绑定到DataGrid的DataTable后选择新行

时间:2012-09-02 20:32:32

标签: c# wpf datagrid wpfdatagrid

我有一个简单的C#程序,其中包含DataTable中的一些数据,这些数据绑定到WPF DataGrid。

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

dataGridEditTab.ItemsSource = NavTable.DefaultView;

我有一个“添加行”按钮,在按下时会向DataTable添加一个空行。空白行显示在DataGrid上,但未选中。我想自动选择新行,但我无法弄清楚如何做到这一点。我想我需要将SelectedItem设置为某些东西,但我还没弄清楚是什么。有帮助吗?感谢。

2 个答案:

答案 0 :(得分:0)

根据此帖子(How to set selected row of DataGridView to newly-added row when the grid is bound to sorted DataView),您可以使用DataGridViewRowsAddedEventArgs完成此操作。基本上创建一个全局变量来放置新添加的行索引。在EventArgs中为该索引分配适当的值,然后使用该索引选择要查找的行。

答案 1 :(得分:0)

如果是MVVM,则使用SelectedItem或SelectedIndex的绑定。如果没有,请更好地使用它)

要实现目标,只需绑定SelectedIndex,每当添加行时,将SelectedIndex设置为RowsCount-1;

这是一个例子。

<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>

和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尝试