WPF DG-如何从DataGrid中删除Button click事件上的选定行

时间:2014-11-02 10:00:07

标签: wpf wpfdatagrid

我有以下问题。

我通过鼠标点击从我的DataGrid中选择一行,如下所示: Admin admin =(Admin)dGrid.SelectedItem;

如何使用按钮点击事件删除此行?我无法使用Rows.Delete并且没有在WPF中找到任何删除,删除命令。谢谢你的帮助!

3 个答案:

答案 0 :(得分:0)

这实际上取决于您将数据绑定到DataGrid的方式。

由于您看起来绑定了可枚举的管理员类型,因此您应该在列表中找到它并将其删除。然后DataGrid应该更新。

答案 1 :(得分:0)

如果要将DataGrid绑定到可观察集合(以两种方式模式),则从集合中删除所选项目就是您需要做的所有事情; 以下是如何继续(DoubleClick To delete)的示例:

 <Grid>     
    <DataGrid ItemsSource="{Binding AdminCollection,Mode=TwoWay}" SelectedItem="{Binding SelectedAdmin}" MouseDoubleClick="DeleteRowEvent" >           
    </DataGrid>        
</Grid>

以及相应的代码:

 public partial class MainWindow : Window, INotifyPropertyChanged
{
    private Admin _selectedAdmin = new Admin();
    public Admin SelectedAdmin
    {
        get { return _selectedAdmin; }
        set
        {
            if (_selectedAdmin == value) return;
            _selectedAdmin = value;
            OnPropertyChanged();
        }
    }

    private ObservableCollection<Admin> _adminCollection = new ObservableCollection<Admin>();
    public ObservableCollection<Admin> AdminCollection
    {
        get { return _adminCollection; }
        set
        {
            if (_adminCollection == value) return;
            _adminCollection = value;
            OnPropertyChanged();
        }
    }
    public MainWindow()
    {
        InitializeComponent();
        AdminCollection = new ObservableCollection<Admin>()
    {
        new Admin()
        {
            Name = "James",Age = 34, Location = "Paris"
        },
         new Admin()
        {
            Name = "Joe",Age = 34, Location = "Us"
        },
         new Admin()
        {
            Name = "Ann",Age = 34, Location = "Canada"
        },
    };
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

    private void DeleteRowEvent(object sender, MouseButtonEventArgs e)
    {
        AdminCollection.Remove(AdminCollection.First(x => x.Id == SelectedAdmin.Id));
    }
}
public class Admin
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Location { get; set; }
    public int Age { get; set; }
}

DataContext设置为MainWindow.xaml中的代码

DataContext="{Binding RelativeSource={RelativeSource Self}}"

答案 2 :(得分:0)

如果您手动创建数据网格并使用linq,那么你可以试试

像那样: -

<Window x:Class="Wpf_grid.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="326" Width="946">
<Grid>
    <DataGrid Name="MyDataGrid" Uid="MyDataGrid" AutoGenerateColumns="False" AlternationCount="2" SelectionMode="Single" Margin="0,31,0,0" IsReadOnly="False" >
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=RegId}" IsReadOnly="False" Header="Registration Id" Width="sizeToHeader"/>
            <DataGridTextColumn Binding="{Binding Path=Name}" IsReadOnly="False" Header="Name" Width="sizeToHeader" />

         <--Many column here-->

            <DataGridTemplateColumn Header="Edit Row">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Edit" Click="btnEdit_Click"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Delete Row">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Delete" Click="btnDelete_Click"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>   
</Grid>

这里是.cs代码

 private void btnDelete_Click(object sender, RoutedEventArgs e)
    {
        gridDataContext gcdd = new gridDataContext();
        registration RegistrationRow = MyDataGrid.SelectedItem as registration;
        var Registration = (from p in gcdd.registrations where p.RegId == RegistrationRow.RegId select p).Single();
        gcdd.registrations.DeleteOnSubmit(Registration);
        gcdd.SubmitChanges();
        MessageBox.Show("Row Deleted Successfully");
        LoadCustomerDetail();
    }