本文介绍了在WPF中没有使用DataGrid刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 对不起,我已经在这里看到过几次被问到的问题,但是没有一个答案能解决我的问题。 public MainWindow() { _PropertyTenantData = new DataTable(); _PropertyTenantData.Columns.Add(new DataColumn( Property,typeof(string))); _PropertyTenantData.Columns.Add(new DataColumn( Tenant,typeof(string)));Sorry, I have seen this questioned asked a couple of times here already, but none of the answers have solved my problem. public MainWindow() { _PropertyTenantData = new DataTable(); _PropertyTenantData.Columns.Add(new DataColumn("Property", typeof(string))); _PropertyTenantData.Columns.Add(new DataColumn("Tenant", typeof(string))); DBConnect RentalDatabase = new DBConnect(); List<string>[] list = new List<string>[2]; list = RentalDatabase.SelectPropertyTenant(); var row = _PropertyTenantData.NewRow(); int numberOfIndividuals = RentalDatabase.CountIndividuals(); for(int x = 0; x < numberOfIndividuals; x++) { row = _PropertyTenantData.NewRow(); _PropertyTenantData.Rows.Add(row); row["Property"] = list[0][x]; row["Tenant"] = list[1][x]; } InitializeComponent(); }这是更改数据的事件。 private void Button_Click(对象发送者,RoutedEventArgs e) { 字符串tenantFirstName = FirstName.Text; 字符串tenantLastName = LastName.Text; 字符串street = Street.Text; 字符串邮政编码= Zipcode.Text; 字符串city = City.Text; DBConnect RentalDatabase = new DBConnect(); RentalDatabase.InsertNewTenant(tenantFirstName, tenantLastName); RentalDatabase.InsertNewProperty(街道,邮政编码,城市); RentalDatabase.InsertNewLease(tenantFirstName,tenantLastName,街道); FirstName.Clear( ); LastName.Clear(); Street.Clear(); Zipcode.Clear(); Ci ty.Clear(); }And this is the event that is changing the data. private void Button_Click(object sender, RoutedEventArgs e) { String tenantFirstName = FirstName.Text; String tenantLastName = LastName.Text; String street = Street.Text; String zipcode = Zipcode.Text; String city = City.Text; DBConnect RentalDatabase = new DBConnect(); RentalDatabase.InsertNewTenant(tenantFirstName, tenantLastName); RentalDatabase.InsertNewProperty(street, zipcode, city); RentalDatabase.InsertNewLease(tenantFirstName, tenantLastName, street); FirstName.Clear(); LastName.Clear(); Street.Clear(); Zipcode.Clear(); City.Clear(); }然后从MySQL获取数据。And it's getting the data from MySQL.推荐答案您是否尝试过重置 DataContext DataGrid 中的$ c>更新到 DataTable 之后?Have you tried resetting the DataContext of your DataGrid to your DataTable after it has been updated? 更新您的DataTable应该是DataGrid的DataContext。看来您正在将DataGrid绑定到DataTable。Your DataTable should be the DataContext of your DataGrid. It looks like you are binding your DataGrid to your DataTable.myGrid.DataContext = myTable;看起来您当前正在直接更新到数据库,而不是数据表。您需要更新您的数据表,然后提交到您的数据库。完成此操作后,将您的DataContext重置为更新后的Table。它应该将您的DataGrid刷新为更新的数据。It looks like you are currently updating straight to your DataBase, instead of the DataTable. You need to update your DataTable, and then commit to your DataBase. Once this is done, reset your DataContext your updated Table. It should refresh your DataGrid to the updated data. 这篇关于在WPF中没有使用DataGrid刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-24 16:06