2013-08-01 22 views
0

我正在使用wpf和devexpress工具,我製作了一個小應用程序,我在其中創建了ADO.net Connectivity並在數據庫中顯示了名稱表中的所有名稱。我的問題是:這裏在代碼中有一個刪除按鈕功能,它描述了一個重點行的刪除,現在當我點擊那個按鈕重點行被刪除,但沒有更改數據庫中的表,我應該寫在這裏刪除按鈕功能從數據庫中刪除重點行。任何人都可以回答我這個查詢。 謝謝, 安妮如何從wpf數據庫中刪除重點行

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> 

回答

0

是否使用綁定顯示在網格中的記錄?刪除需要發生在實體而不是實際的網格上。

+0

不,我沒有使用結合我已經貼上:) –

0

您可以使用: 當您從鍵盤按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; 
       } 

在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); 
     } 
+0

上述,我想從數據庫中刪除的行XAML代碼。 –

+0

已編輯的代碼請檢查 –