【问题标题】:How to update cell value in UWP Microsoft Toolkit DataGrid?如何更新 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();
       }
    }

【问题讨论】:

    标签: c# xaml uwp win-universal-app windows-community-toolkit


    【解决方案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的输入验证再次感谢
    猜你喜欢
    • 2022-01-15
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多