【问题标题】:DataGrid sample in WPF using visual studio 2012使用 Visual Studio 2012 的 WPF 中的 DataGrid 示例
【发布时间】:2013-10-17 13:49:30
【问题描述】:

我有以下 XAML 代码:

<Window x:Class="simpledatagrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="IDDATA" Height="350" Width="525">
    <Grid>
        <DataGrid  Name="dgsample" BorderBrush="Black" BorderThickness="2" AutoGenerateColumns="True" CanUserAddRows="True" CanUserDeleteRows="True"  Margin="200,10,10,75"/>

        <Label  Content="ID :" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="26" Width="27"/>
        <Label  Content="Name :" HorizontalAlignment="Left" Margin="10,60,0,0" VerticalAlignment="Top" Height="26" Width="48"/>
        <Label  Content="Salary :" HorizontalAlignment="Left" Margin="10,110,0,0" VerticalAlignment="Top" Height="26" Width="47"/>

        <TextBox Name="tb1" HorizontalAlignment="Left" Height="20" Margin="60,10,0,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="100" />
        <TextBox Name="tb2" HorizontalAlignment="Left" Height="20" Margin="60,60,0,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="100"/>
        <TextBox Name="tb3" HorizontalAlignment="Left" Height="20" Margin="60,110,0,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="100"/>

        <Button Content="Get" HorizontalAlignment="Left" Margin="10,190,0,0" VerticalAlignment="Top" Width="75" Click="Get_Click" />
        <Button Content="Add" HorizontalAlignment="Left" Margin="10,230,0,0" VerticalAlignment="Top" Width="75" Click="Add_Click" />
        <Button Content="Delete" HorizontalAlignment="Left" Margin="10,270,0,0" VerticalAlignment="Top" Width="75" Click="Delete_Click" />
    </Grid>
</Window>

这是.cs代码

public partial class MainWindow : Window
{
    ObservableCollection<User> Users = new ObservableCollection<User>();
    public MainWindow()
    {
        InitializeComponent();

        Users.Add(new User() { Id = 101, Name = "Allen", Salary = 10 });
        Users.Add(new User() { Id = 102, Name = "king", Salary = 20 });
        Users.Add(new User() { Id = 103, Name = "scot", Salary = 30 });
        Users.Add(new User() { Id = 104, Name = "havy", Salary = 40 });
        Users.Add(new User() { Id = 105, Name = "xen", Salary = 50 });
        Users.Add(new User() { Id = 106, Name = "len", Salary = 60 });

        dgsample.ItemsSource = Users;
   }

    private void Get_Click(object sender, RoutedEventArgs e)
    {            
        {

            User currentUser = Users.Single(select => select.Id == int.Parse(this.tb1.Text));
            this.tb2.Text = currentUser.Name;
            this.tb3.Text = currentUser.Salary.ToString();

        }
     }

    private void Add_Click(object sender, RoutedEventArgs e)
    {
        Users.Add(new User() { Id = int.Parse(tb1.Text), Name = tb2.Text, Salary = int.Parse(tb3.Text) });
    }

    private void Delete_Click(object sender, RoutedEventArgs e)
    {
        Users.RemoveAt(dgsample.SelectedIndex);                       
    }
}

此代码正在运行,现在我需要的是,如果我输入了不在 DatGrid 中的错误 ID,它应该在 MessageBox 中显示为无效,并且如果我在 TextBox 中输入 ID 并单击它应该得到的删除按钮删除..请帮我处理我对 WPF 不熟悉的代码

【问题讨论】:

    标签: c# wpf xaml visual-studio-2012 datagrid


    【解决方案1】:

    通过不使用索引来执行删除,而是使用实际对象,将删除更改为更具体。还要尝试通过始终检查空情况来计划任何错误,甚至是开发人员的错误。

    private void Delete_Click(object sender, RoutedEventArgs e)
    {
    
        if (dgsample.SelectedItem != null)
        {
            var usr = dgsample.SelectedItem as User;
    
            if (usr != null)
            {
                var deleteMe = Users.FirstOrDefault(us => us.Id == usr.Id);
                if (deleteMe != null)
                {
                    Users.Remove(deleteMe);
                }
                else
                {
                    MessageBox.Show(string.Format("User with Id {0} not found to delete", deleteMe.Id);
                }
            }
            else
            {
                MessageBox.Show("Unknown type in datagrid")
            }
        }
        else
        {
            MessageBox.Show("No user selected to delete");
        }
    
    }
    

    【讨论】:

    • @user2889489 上面的代码只会在删除点击事件期间捕获错误的 ID。您需要对事件进行 id 检查,例如当 ID 文本框失去焦点时,然后检查。
    猜你喜欢
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多