Doesn't update in WPF using DataGrid

Sorry, I saw this question asked a couple of times here, but none of the answers resolved 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 changes 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 gets data from MySQL.

0


source to share


1 answer


Have you tried resetting DataContext

your DataGrid

on DataTable

after updating it?

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 directly to your database and not to the DataTable. You need to update the DataTable and then commit your database. Once that's done, resetting your DataContext will update the table. It should update the DataGrid to the updated data.

0


source







All Articles