wpf DataGrid 删除多行
各位老师好!
在界面上添加一个DataGrid控件,绑定一个DataTable数据源。
this.datagrid.ItemsSource = table.DefaultView;
<DataGrid HorizontalAlignment="Left" Margin="12,71,0,150" Name="datagrid" Width="853" CanUserAddRows="false" AutoGenerateColumns="True" LayoutUpdated="datagrid_LayoutUpdated" Loaded="datagrid_Loaded" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="选择">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate />
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Checked="CheckBox_Checked" Tag="{Binding Path= id}" Name="select"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
foreach (var item in datagrid.Items)
{
DataGridTemplateColumn templeColumn = datagrid.Columns[0] as DataGridTemplateColumn;
FrameworkElement s = datagrid.Columns[0].GetCellContent(item);
DataView view = new DataView();
System.Windows.Controls.CheckBox cb = templeColumn.CellTemplate.FindName("select", s) as System.Windows.Controls.CheckBox;
if (cb.IsChecked == true)
{
System.Windows.MessageBox.Show(Convert.ToString(cb.Tag));
datagrid.Items.Remove(item);
}
}