繁体   English   中英

删除 WPF 浏览器应用程序的 DataGrid 中的行

[英]Delete Row In a DataGrid for a WPF browser Application

在使用了很多 WinForms 之后,我正在尝试构建一个 WPF 浏览器应用程序。 我没有找到删除 dataGrid 中一行的方法。 更新和插入部分工作正常。 我已经阅读了很多关于 ObservableCollection 的答案,但我认为它不适合我的情况。

我试过这个:

foodSupplierDataGrid.Items.Remove(foodSupplierDataGrid.SelectedItem);

但它给出了一个错误。

使用 ItemsSource 时操作无效。 改为使用 ItemsControl.ItemsSource 访问和修改元素。

所以我该怎么做?

XAML

<

<Page

    <Page.Resources>
        <local:larotis_general_testDataSet x:Key="larotis_general_testDataSet"/>
        <CollectionViewSource x:Key="foodSupplierViewSource" Source="{Binding foodSupplier, Source={StaticResource larotis_general_testDataSet}}"/>
    </Page.Resources>

    <Grid Background="#FF066E19" Margin="-463,-198,-465,-234">
        <dg:DataGrid x:Name="foodSupplierDataGrid" AutoGenerateColumns="True" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="296,134,288,224" RowDetailsVisibilityMode="VisibleWhenSelected" Loaded="foodSupplierDataGrid_Loaded" CanUserDeleteRows="True" />
        <Menu>
            ...
        </Menu>
        <Button x:Name="Delete" Content="Button" HorizontalAlignment="Left" Margin="933,836,0,0" VerticalAlignment="Top" Width="75" Click="Delete_Click"/>
    </Grid>


</Page>

背后的代码

public partial class Supplier : Page
{
        larotis_general_testDataSetTableAdapters.foodSupplierTableAdapter adapter = new larotis_general_testDataSetTableAdapters.foodSupplierTableAdapter();
        larotis_general_testDataSet dataset = new larotis_general_testDataSet();

public Supplier() { InitializeComponent(); } private void foodSupplierDataGrid_Loaded(object sender, RoutedEventArgs e) { adapter.Fill(dataset.foodSupplier); this.DataContext = dataset.foodSupplier.DefaultView; dataset.foodSupplier.foodSupplierRowChanged += new larotis_general_testDataSet.foodSupplierRowChangeEventHandler(SupplierRowModified); dataset.foodSupplier.foodSupplierRowDeleted += new larotis_general_testDataSet.foodSupplierRowChangeEventHandler(SupplierRowModified); } void SupplierRowModified(object sender, larotis_general_testDataSet.foodSupplierRowChangeEvent e) { adapter.Update(dataset.foodSupplier); } private void Delete_Click(object sender, RoutedEventArgs e) { } }

终于自己找到了。 这很简单。

 private void Delete_Click(object sender, RoutedEventArgs e)
    {
         dataset.foodSupplier.DefaultView.Delete(foodSupplierDataGrid.SelectedIndex);
         this.DataContext = dataset.foodSupplier.DefaultView;
    }

暂无
暂无

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

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