如何在wpf datagrid中删除所选行(使用复选框)

时间:2014-07-30 19:42:36

标签: c# wpf xaml datagrid wpfdatagrid

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

同样Dynamicaly我正在将数据填充到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;
    } 

如果我错了,请告诉我。

1 个答案:

答案 0 :(得分:0)

我将如何做到这一点。实现继承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完成。