如何更新 UWP Microsoft Toolkit DataGrid 中的单元格值?
Posted
技术标签:
【中文标题】如何更新 UWP Microsoft Toolkit DataGrid 中的单元格值?【英文标题】:How to update cell value in UWP Microsoft Toolkit DataGrid? 【发布时间】:2021-08-10 04:21:44 【问题描述】:我正在尝试从数据网格更新单元格值 但我不知道如何设置新的单元格值
private async void GridViewAccount_CellEditEnded(object sender,
Microsoft.Toolkit.Uwp.UI.Controls.DataGridCellEditEndedEventArgs e)
using (SqlConnection con = new SqlConnection((App.Current as App).Connectionstring))
SqlCommand command = new SqlCommand("sp_updateaccount", con);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@id", accountId);
command.Parameters.AddWithValue("@username", null); //how to get the new cell value
command.Parameters.AddWithValue("@password", null); //how to get the new cell value
command.Parameters.AddWithValue("@phone", null); //how to get the new cell value
command.Parameters.AddWithValue("@role", null); //how to get the new cell value
await con.OpenAsync();
await command.ExecuteNonQueryAsync();
con.Close();
accountToolRefresh();
【问题讨论】:
【参考方案1】:首先,您需要实现双向绑定。如下:
Xaml 代码:
<controls:DataGrid
x:Name="MyDataGrid"
AutoGenerateColumns="False"
CellEditEnded="MyDataGrid_CellEditEnded"
ItemsSource="x:Bind Users,Mode=TwoWay">
<controls:DataGrid.Columns>
<controls:DataGridTextColumn Header="AccountId " Binding="Binding Id,Mode=TwoWay"/>
<controls:DataGridTextColumn Header="UserName" Binding="Binding UserName,Mode=TwoWay"/>
<controls:DataGridTextColumn Header="Password" Binding="Binding Password,Mode=TwoWay"/>
</controls:DataGrid.Columns>
</controls:DataGrid>
后面的代码:
public ObservableCollection<User> Users;
public MainPage()
this.InitializeComponent();
Users=new ObservableCollection<User>
new User()Id=1, UserName=”tom”, password=”123”,
new User()Id=2, UserName=”lily”, password=”563”
public class User:INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public int _id;
public string _username;
public string _password;
public int Id
get return _id;
set
_id = value;
RaisePropertyChanged("Id");
public string UserName
get return _username;
set
_username = value;
RaisePropertyChanged("UserName");
public string PassWord
get return _password;
set
_password = value;
RaisePropertyChanged("PassWord");
public void RaisePropertyChanged(string propertyname = null)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
当您更改单元格值时,Users
集合将被更新。然后在CellEditEnded
事件中,获取当前行的DataContext
,并将其转换为一个对象,这样就可以通过这个对象获取它的属性。
private async void MyDataGrid_CellEditEnded(object sender, DataGridCellEditEndedEventArgs e)
User user=e.Row.DataContext as User;
……
command.Parameters.AddWithValue("@id", user.accountId);
command.Parameters.AddWithValue("@username", user.username);
command.Parameters.AddWithValue("@password", user.password);
【讨论】:
非常感谢我的问题解决了,现在我只需要datagrid的输入验证再次感谢以上是关于如何更新 UWP Microsoft Toolkit DataGrid 中的单元格值?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 VisualStudio 2019 为 Microsoft.Toolkit.Uwp.UI.Controls 中的控件获取 UWP 的默认模板(样式代码)?
无论如何要获取与 UWP 中的购买相关联的 Microsoft Store 帐户电子邮件地址吗?
如何为 UWP 包安装程序添加 Microsoft.NET.CoreRuntime.2.1?