简体   繁体   English

如何从WPF中的数据库中删除焦点行

[英]How to delete a focused row from database in wpf

I am using wpf and devexpress tool, I made small applicationin which i did ADO.net Connectivity and displayed all names from Name table in database. 我正在使用wpf和devexpress工具,我做了一个小型应用程序,在其中进行了ADO.net连接,并显示了数据库中“名称”表中的所有名称。 My question is: Here in code there is a delete button function which describes deletion of a focused row, now as i click that button focused row get deleted but no changes are made to table in database , What should i write here in delete button function to delete that focused row from database too. 我的问题是:在代码中有一个删除按钮功能,它描述了删除关注的行,现在当我单击该按钮时,该行被删除但数据库中的表未进行任何更改,我应该在删除按钮中写什么也从数据库中删除该焦点行。 Can anyone answer my this query. 谁能回答我这个查询。 Thanks, Annie 谢谢安妮

  public partial class MainWindow : Window
    {
    public MainWindow()
        {
        InitializeComponent();

        }

    private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
        nEntities nr = new nEntities();
        nr.Names.ToList();
        this.grid.ItemsSource = nr.Names.Local;


        }
    private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
        if (grid.IsValidRowHandle(view.FocusedRowHandle))
            view.DeleteRow(view.FocusedRowHandle);

        }
 <Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="30" />
    </Grid.RowDefinitions>
    <dxg:GridControl Grid.Row="0" Name="grid" AutoGenerateColumns="AddNew">
        <dxg:GridControl.View>
            <dxg:TableView x:Name="view" NavigationStyle="Cell"
                           NewItemRowPosition="Top" />
        </dxg:GridControl.View>
    </dxg:GridControl>
<Grid Margin="3" Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Button Content="Add Empty Row" />
        <Button Content="Delete Focused Row"
                Click="DeleteButton_Click"
                Grid.Column="1" />
    </Grid>

Are you using binding to show the records in the grid? 您是否正在使用绑定在网格中显示记录? The delete needs to happen on the entities not the actual grid. 删除需要发生在实体上而不是实际的网格上。

You can use: When you press Delete Button from keyboard it will delete. 您可以使用:当您按下键盘上的Delete Button时,它将删除。

private void LayoutRoot_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                MessageBoxResult result = MessageBox.Show("Do you want to delete this record ?", "Delete Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                    view.DeleteRow(view.FocusedRowHandle);
                    e.Handled = true;
                 }

In CellValueChanging Event : 在CellValueChanging事件中:

private void view_CellValueChanging(object sender, CellValueChangedEventArgs e)
        {
            int selectedRowhandle = ((GridViewBase)LayoutRoot.View).GetSelectedRowHandles()[0];
            Employee rowid = ((Employee)LayoutRoot.GetRow(selectedRowhandle));
            //get id and confirmation and then delete
             //Delete(rowid.EmployeeID);
        }

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

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