Reputation: 3
I am trying to update a DataGrid from my sdf file using C# in Visual Studio 2010. The DataGrid is as follows.
<Grid Height="auto" Name="grid1" Width="auto" >
<Grid.RowDefinitions>
<RowDefinition Height="{Binding ElementName=grid1, Path=ActualHeight}"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="200" Width="200" Name="DGWidth"/>
<ColumnDefinition MinWidth="600" Name="PIWidth"/>
</Grid.ColumnDefinitions>
<DataGrid AutoGenerateColumns="True"
Grid.Column="1" Grid.Row="0"
Height="{Binding ElementName=grid1, Path=ActualHeight}"
HorizontalAlignment="Left"
Name="PIView"
VerticalAlignment="Top"
Width="{Binding ElementName=PIWidth, Path=ActualWidth}"
IsReadOnly="True"/>
</Grid>
I am trying to populate the PIView DataGrid. To do that I am passing the DataGrid Reference to a method updatePIView
in a manager class.
public class PTManager
{
public PTManager()
{
}
public void updatePIView(ref DataGrid pIView, DateTime datetime)
{
PTDate date = PTDatabase.GetDt(datetime);
SqlCeDataAdapter adapter = PTDatabase.GetAdaperForPIViewForDt(date);
DataTable table = new DataTable();
adapter.Fill(table);
pIView.ItemsSource = table.DefaultView;
}
}
In side the method I am setting the ItemsSource. It worked for another DataGrid (with a single column table) which was loaded just after loading the main window.
I am trying load data in PIView
on a selection event. I can see the data in the table. But when the default view of the table is set as the ItemsSource
nothing happens. WPF does not produce any error or warning.
Am I missing something?
Upvotes: 0
Views: 3041
Reputation: 69959
Please don't use WPF as if it were Windows Forms... it's not Windows Forms.
In WPF, we use the DataGrid
by loading data in a suitable form into a property and data bind that property to the DataGrid.ItemsSource
property like this:
<DataGrid ItemsSource="{Binding YourCollectionProperty}" ... />
...
// preferably implement `INotifyPropertyChanged` interface on this property
public ObservableCollection<YourDataType> YourCollectionProperty { get; set; }
...
YourCollectionProperty = new ObservableCollection<YourDataType>(GetData());
If you are unfamiliar with data binding in WPF, please refer to the Data Binding Overview page on MSDN... you also need to correctly set the DataContext` of your view to an instance of the class that has the property to data bind to for example:
DataContext = new SomeViewModelWithTheBindableProperty();
Upvotes: 1