使用DataGrid在WPF中不刷新

时间:2010-08-06 21:37:56

标签: c# wpf datagrid refresh wpfdatagrid

对不起,我已经看到过这个问题已经在这里问了几次,但没有一个答案解决了我的问题。
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();
    }

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(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获取数据。

1 个答案:

答案 0 :(得分:0)

您是否尝试在DataContext更新后DataGrid重置DataTable

<强>更新

您的DataTable应该是DataGrid的DataContext。看起来您正在将DataGrid绑定到DataTable。

myGrid.DataContext = myTable;

您好像正在直接更新到DataBase而不是DataTable。您需要更新DataTable,然后提交到DataBase。完成此操作后,将DataContext重置为更新的表。它应该将DataGrid刷新为更新的数据。