C# 如何在UWP Microsoft Toolkit DataGrid中更新单元格值?
我正在尝试从datagrid更新单元格值 但是我不知道如何设置新的单元格值C# 如何在UWP Microsoft Toolkit DataGrid中更新单元格值?,c#,xaml,uwp,win-universal-app,windows-community-toolkit,C#,Xaml,Uwp,Win Universal App,Windows Community Toolkit,我正在尝试从datagrid更新单元格值 但是我不知道如何设置新的单元格值 private async void GridViewAccount_CellEditEnded(object sender, Microsoft.Toolkit.Uwp.UI.Controls.DataGridCellEditEndedEventArgs e) { using (SqlConnection con = new SqlConnection((App.Current
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();
}
}
首先,您需要实现双向绑定。详情如下: 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>
非常感谢您解决了我的问题,现在我只需要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));
}
}
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);
}