Update DataGrid bound to DataTable after DataTable is altered WPF
I have a combobox where the user can select a particular year (populated by the database) and view particular information for the year. That information is stored in a datatable which is bound to a datagrid. I want it to recreate the data grid when I change the year.
_PropertyTenantData
is a DataTable that has 14 columns Property Id, Property and year + Month. FillDataGrid creates the columns of _PropertyTenantData
with a given year, and runs a MySQL query which fills its rows. When the selection in the ComboBox changes I want to refresh the DataGrid because it will have new column names and different data.
public ViewCurrentPropertyTenant : Window
{
FillComboBox();
FillDataGrid("2010");
InitializeComponent();
}
//...
private void cboYear_SelectionChangd(object sender, SelectionChangedEventArgs e)
{
object selectedItem = cboYea开发者_如何学运维r.SelectedItem;
string year = (selectedItem as YearData).Year;
if(!String.IsNullOrEmpty(year))
{
_PropertyTenantData.Reset();
FillDataGrid(year);
myGrid.Items.Refresh();
}
}
The first part runs fine and gives me the data as I want it structured. The second part removes all the rows, the columns stay the same, and the new rows of _PropertyTenantData
are not added to the dataGrid. Thanks for the help!
Nevermind, I found the answer. Instead of refreshing I just did this:
myGrid.ItemsSource = _PropertyTenantData.DefaultView;
精彩评论