问题
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();
}
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();
}
And it's getting the data from MySQL.
回答1:
Have you tried resetting the DataContext
of your DataGrid
to your DataTable
after it has been updated?
Update
Your DataTable should be the DataContext of your DataGrid. It looks like you are binding your DataGrid to your DataTable.
myGrid.DataContext = myTable;
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.
来源:https://stackoverflow.com/questions/3427979/not-refreshing-in-wpf-with-datagrid