使用 VisualStudio C# 的 WPF 中的 DataGrid
本文关键字:中的 DataGrid WPF VisualStudio 使用 | 更新日期: 2023-09-27 18:35:54
我的窗口中有 3 个TexBox
es、Button
s 和 DataGrid
。 当我将数据输入TextBox
并单击按钮时,它应该添加到DataGrid
中。我需要有关如何添加、删除和获取数据的代码。我是 wpf 的新手,请帮我编写代码。这是我的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 BorderBrush="Black" BorderThickness="2" AutoGenerateColumns="True" Name="dgsample" 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>
这是我.cs代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<User> users = new List<User>();
users.Add(new User() { Id = 101, Name = "gin", Salary = 10 });
users.Add(new User() { Id = 102, Name = "alen", Salary = 20 });
users.Add(new User() { Id = 103, Name = "scott", Salary = 30 });
dgsample.ItemsSource = users;
}
private void Get_Click(object sender, RoutedEventArgs e)
{
}
private void Add_Click(object sender, RoutedEventArgs e)
{
}
private void Delete_Click(object sender, RoutedEventArgs e)
{
}
}
}
而不是 List。 因为 ObservableCollection 默认具有 INotifyCollectionChanged。ObservableCollection 是 TwoWay Binding。
ObservableCollection<User> users = new ObservableCollection<User>();
public MainWindow()
{
InitializeComponent();
users.Add(new User() { Id = 101, Name = "gin", Salary = 10 });
users.Add(new User() { Id = 102, Name = "alen", Salary = 20 });
users.Add(new User() { Id = 103, Name = "scott", Salary = 30 });
dgsample.ItemsSource = users;
}
private void Get_Click(object sender, RoutedEventArgs e)
{
if (this.tb1.Text != string.Empty) { User currentUser = users.Single(select => select.Id == Int32.Parse(this.tb1.Text)); this.tb2.Text = currentUser.Name; this.tb3.Text = currentUser.Salary; }
}
private void Add_Click(object sender, RoutedEventArgs e)
{
users.Add(new User() { Id = 105, Name = "gin5", Salary = 100 });
}
private void Delete_Click(object sender, RoutedEventArgs e)
{
users.RemoveAt(0);
}
定义要设置为DataGrid
ItemsSource
的 ObservableCollection
属性,并在要从DataGrid
中添加/删除任何项目时更新它。更新集合将更新数据网格
public MainWindow()
{
InitializeComponent();
Users = new ObservableCollection<User>;
Users.Add(new User() {Id = 101, Name = "gin", Salary = 10});
Users.Add(new User() {Id = 102, Name = "alen", Salary = 20});
Users.Add(new User() {Id = 103, Name = "scott", Salary = 30});
dgsample.ItemsSource = Users;
}
private ObservableCollection<User> Users { get; set; }
private void Add_Click(object sender, RoutedEventArgs e)
{
Users.Add(new User() {Id = int.Parse(tb1.Text), Name = tb2.Text, Salary = tb3.Text});
}
此外,作为建议,添加/删除功能是内置的DataGrid
,将CanUserAddRows
和CanUserDeleteRows
设置为 true 允许用户将项目直接添加到DataGrid
中,因此您可能不需要文本框和按钮来添加/删除
<DataGrid AutoGenerateColumns="True" CanUserAddRows="True" CanUserDeleteRows="True"></DataGrid>
如果你是WPF的新手,你应该从WPF的方式开始。在 WPF 中,应操作绑定到 UI 的数据,而不是直接操作 UI。此设计模式称为 MVVM。
在您的情况下,您将拥有一个类(称为 ViewModel),该类将包含User
项(称为模型)的集合。
public class MainWindowViewModel
{
public MainWindowViewModel()
{
Users = new ObservableCollection<User>();
users.Add(new User() { Id = 101, Name = "gin", Salary = 10 });
users.Add(new User() { Id = 102, Name = "alen", Salary = 20 });
users.Add(new User() { Id = 103, Name = "scott", Salary = 30 });
}
public ObservableCollection<User> Users { get; set; }
}
在您的MainWindow
(称为视图)代码隐藏中,您应该做的只是将其DataContext
设置为MainWindowViewModel
。
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
在窗口本身中,将集合绑定到 ViewModel 中的Users
属性
<DataGrid BorderBrush="Black" BorderThickness="2" AutoGenerateColumns="True" Name="dgsample" Margin="200,10,10,75" ItemsSource="{Binding Users}"></DataGrid>
至于您的TextBox
,因为它们旨在装箱新User
,您可以在ViewModel上拥有另一个User
属性
public User NewUser { get; set; }
并将它们绑定到User
类中的匹配属性
<TextBox Name="tb1" HorizontalAlignment="Left" Height="20" Margin="60,10,0,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="100" Text="{Binding Path=NewUser.Id}"/>
<TextBox Name="tb2" HorizontalAlignment="Left" Height="20" Margin="60,60,0,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="100" Text="{Binding Path=NewUser.Name}"/>
<TextBox Name="tb3" HorizontalAlignment="Left" Height="20" Margin="60,110,0,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="100" Text="{Binding Path=NewUser.Salary}"/>
添加新User
后,清除其属性。
至于您的按钮,它们应该通过命令与 ViewModel 通信,然后 ViewModel 仅使用它实现的简单Add
和Remove
方法来操作集合本身。您可以在此处了解更多信息
希望这有帮助
定义用户类:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public User()
{
}
public User(int id, string name, int salary)
{
Id = id;
Name = name;
Salary = salary;
}
}
然后在 MainWindow.xaml 中.cs将列表更改为 ObservableCollection(您需要添加;
using System.Collections.ObjectModel;
//编辑
然后:
ObservableCollection<User> users;
public MainWindow()
{
users = new ObservableCollection<User>();
users.Add(new User() { Id = 101, Name = "gin", Salary = 10 });
users.Add(new User() { Id = 102, Name = "alen", Salary = 20 });
users.Add(new User() { Id = 103, Name = "scott", Salary = 30 });
InitializeComponent();
dgsample.ItemsSource = users;
}
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);
}
// you can get what you need just selected proper User proprety
private void Get_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(( (User)dgSample.Items[dgSample.SelectedIndex]).Name);
}