使用visual studio 2012在WPF中使用DataGrid示例 [英] DataGrid sample in WPF using visual studio 2012
本文介绍了使用visual studio 2012在WPF中使用DataGrid示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下Xaml代码
I have following Xaml code
<code><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"></DataGrid>
<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></code>
这是.cs代码
This is .cs code
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);
}
}
此代码正常运行,现在我需要的是如果我输错了它应该在MessageBox中显示不在DatGrid中的ID无效,如果我在TextBox中输入ID并单击删除按钮,它应该被删除..请帮助我使用我刚接触WPF的代码
This code is working,now i need is if i enter wrong ID which is not in the DatGrid it should show in the MessageBox as invalid and also if i enter the ID in the TextBox and click on the delete button it should get delete..please help me with the code I'm new to WPF
推荐答案
你的问题与WPF无关。
你在Get_Click方法上需要检查是否用户输入了有效的ID。这样的事情:
Your question has nothing to do with WPF.
On you Get_Click method you need to check if the user has entered a valid ID. somthing like this:
private void Get_Click(object sender, RoutedEventArgs e)
{
int index;
if (int.TryParse(this.tb1.Text, out index))
{
User currentUser = Users.FirstOrDefault(u => u.Id == index);
if (currentUser != null)
{
this.tb2.Text = currentUser.Name;
this.tb3.Text = currentUser.Salary.ToString();
}
else
MessageBox.Show("User with the provided ID does not Exist", "Error");
}
else
MessageBox.Show("ID entered is not valid number", "Error");
}
您对删除方法进行类似检查,方法是检查用户是否输入了有效的ID,如果是,则删除该项目。
you do a similar check on the delete method, by checking if the user has entered a valid id and if yes then delete the item.
这篇关于使用visual studio 2012在WPF中使用DataGrid示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文