接上一篇,本节我们演示如何删除DataGrid中的记录.
功能描述:我们提供一个删除按钮,点击它可以删除当前选中的数据记录行,当然,我们也可以用Delete键盘键删除选中的记录。
一、界面修改
如同添加新记录一样,首先修改用户界面.Page.xaml代码如下:
<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="SLApplicationExample.Page"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width
="400" Height="300">
    
<Grid x:Name="LayoutRoot" Background="White" Margin="5">
        
<Grid.RowDefinitions>
            
<RowDefinition Height="Auto"/>
            
<RowDefinition Height="*"/>
        
</Grid.RowDefinitions>
        
<StackPanel Orientation="Horizontal">
            
<Button x:Name="addButton" Content="Add" Margin="10"/>
            
<Button x:Name="deleteButton" Content="Delete" Margin="10"/>

        
</StackPanel>
        
<data:DataGrid x:Name="dgPeople" Grid.Row="1" />
    
</Grid>

</UserControl>
新用户界面如下图:                       

                        SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录
二、删除按钮点击事件和键盘Delete键事件

事先要绑定事件到DataGrid控件上
            this.deleteButton.Click += new RoutedEventHandler(deleteButton_Click);
            
this.dgPeople.KeyDown += new KeyEventHandler(peopleDataGrid_KeyDown);
1、删除按钮点击事件
  #region 通过按钮删除记录
        
void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            DeletePerson();
        }
        
#endregion
2、Delete键事件
3、建立DeletePerson()代码
  #region 删除记录子程序
        
private void DeletePerson()
        {
            
if (null == this.dgPeople.SelectedItem)
            {
                
return;
            }
            Person person 
= this.dgPeople.SelectedItem as Person;
            
if (null == person)
            {
                
return;
            }
            mypeople.Remove(person);
        }
        
#endregion
 Page.xaml.cs全部代码如下:
SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录using System;
SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录
using System.Collections.Generic;
SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录
using System.Linq;
SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录
using System.Net;
SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录
using System.Windows;
SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录
using System.Windows.Controls;
SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录
using System.Windows.Documents;
SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录
using System.Windows.Input;
SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录
using System.Windows.Media;
SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录
using System.Windows.Media.Animation;
SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录
using System.Windows.Shapes;
SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录
SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录
namespace SLApplicationExample
按F5运行测试即可看到效果
                        SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录

相关文章:

  • 2021-10-09
  • 2022-02-19
  • 2022-01-18
  • 2021-10-13
  • 2021-06-11
  • 2021-05-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-29
  • 2022-01-22
  • 2021-10-07
  • 2021-06-29
  • 2021-05-18
  • 2022-01-05
  • 2021-07-24
相关资源
相似解决方案