繁体   English   中英

如何在WPF DataGrid中删除选定的行(使用复选框)

[英]How to delete selected rows (using checkbox) in wpf datagrid

我的WPF DataGrid是:

<dg:DataGrid Name="datagrid1"  Grid.RowSpan="1"  VerticalAlignment="Stretch" Grid.ColumnSpan="2">

    <dg:DataGrid.Columns >
        <dg:DataGridTemplateColumn>
            <dg:DataGridTemplateColumn.Header>
                <CheckBox Content=" Slect All" x:Name="headerCheckBox" />
            </dg:DataGridTemplateColumn.Header>
            <dg:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox Name="chkSelectAll" Margin="45 2 0 0"
      IsChecked="{Binding IsChecked, ElementName=headerCheckBox, 
                          Mode=OneWay}" />
                </DataTemplate>
            </dg:DataGridTemplateColumn.CellTemplate>
        </dg:DataGridTemplateColumn>

    </dg:DataGrid.Columns>
</dg:DataGrid>

我也动态地将数据填充到datgrid中。在xaml.cs文件中,我编写了以下给定的代码,用于从数据网格中删除所选行,但它将错误抛出行

DataGridRow item =(DataGridRow) datagrid1.ItemContainerGenerator.ContainerFromItem(datagrid1.Items[j]);

因此,请查看下面为我编写的相同代码。

   private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        for (int j = 0; j < datagrid1.Items.Count; j++)
        {
            DataGridRow item =(DataGridRow) datagrid1.ItemContainerGenerator.ContainerFromItem(datagrid1.Items[j]);
            CheckBox ckb = (CheckBox)GetVisualChild<CheckBox>(item);
            if (ckb.IsChecked.Value)
            {
                DataRowView drv = (DataRowView)datagrid1.Items[j];
               //delete the row- updating to the database
            }
        }
    }
    static T GetVisualChild<T>(Visual parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
            {
                child = GetVisualChild<T>(v);
            }
            if (child != null)
            {
                break;
            }
        }
        return child;
    } 

如果有错,请告诉我。

这就是我要怎么做。 实现一个继承INotifyPropertyChanged的类的ObservableCollection。 如果要更新集合中的项目,将使用INotifyPropertyChanged。

首先是GridView的xaml

<DataGrid x:Name="gvMain" AutoGenerateColumns="True" HorizontalAlignment="Left"
        VerticalAlignment="Top" Height="300" Width="300"></DataGrid>

我们的物品类

public class MyClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string firstName { get; set; }
    private string lastName { get; set; }

    public string FirstName
    {
        get 
        {
            return firstName;
        }
        set
        {
            firstName = value;
            PropertyChangedEvent("FirstName");
        }
    }

    public string LastName
    {
        get
        {
            return lastName;
        }
        set
        {
            lastName = value;
            PropertyChangedEvent("LastName");
        }
    }

    private void PropertyChangedEvent(string propertyName)
    {

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

接下来的WPF窗口

public ObservableCollection<MyClass> gridData { get; set; }

public MainWindow()
{
    InitializeComponent();
    gridData = new ObservableCollection<MyClass>();
    gvMain.ItemsSource = gridData;
}

测试添加,更改,删除集合中的项目

private void btnAdd_Click(object sender, RoutedEventArgs e)
{
    gridData.Add(new MyClass() { FirstName = "John", LastName = "Smith"  });
}

private void btnChange_Click(object sender, RoutedEventArgs e)
{
    gridData[0].FirstName = "Meow Mix";
}

private void btnDelete_Click(object sender, RoutedEventArgs e)
{
    //using List to use .ForEach less code to write and looks cleaner to me
    List<MyClass> remove = gridData.Where(x => x.LastName.Equals("Smith")).ToList();
    remove.ForEach(x => gridData.Remove(x));
}

您要进行的任何更改都将使用gridData完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM