开发者

Not refreshing in WPF with DataGrid

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 = Ci开发者_StackOverflowty.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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜