I am using app where I got one window called MainWindow.xaml and on my window I have DataGrid
which is used to show some items from database, My DataGrid took 90% place of my MainWindow.Xaml, its simply all over the screen.
And even If I have like 20 items in DataGrid
I need aproximately 1,60 ( sometimes 2 seconds, sometimes 1,60 seconds, sometimes 1,45 seconds ) seconds to open that MainWindow.xaml. And I can only imagine what's gonna happen on 200+ items.
I tried a lot of stuffs to increase performance but that did not helped me a lot.
Before I list what I've tried I will write here how I'm binding data (I'm doing it from code behind).
List<Product> myList =ProductsController.GetAll();
dtgProducts.ItemsSource = myList;
Now I will list what I have tried allready:
ScrollViewer.CanContentScroll="False"
EnableRowVirtualization ="True"
EnableColumnVirtualization = "True"
AutoGenerateColumns="False"
I even set MaxWidth and MaxHigh to some unreal dimensions just to keep it fixed, because I read somehwere that might help:
MaxWidth="4000" MaxHeight="2000"
And now here is full code :
<DataGrid Grid.Row="1" IsReadOnly="True" Name="dtgProductsts" ScrollViewer.CanContentScroll="False" MaxWidth="4000" MaxHeight="2000" EnableRowVirtualization ="True" EnableColumnVirtualization = "True" HorizontalGridLinesBrush="#d3d3d3" AlternatingRowBackground="#E0E4E5" AlternationCount="2" GridLinesVisibility="Horizontal" FontSize="16" RowHeight="30" SelectionUnit="FullRow" Background="White" Margin="5,0" AutoGenerateColumns="False" RowHeaderWidth="0" VerticalGridLinesBrush="#0091EA" CanUserAddRows="False">
<DataGrid.CellStyle>
<StaticResource ResourceKey="DataGridCentering"/>
</DataGrid.CellStyle>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="#0091EA"/>
<Setter Property="Opacity" Value="1"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="FontSize" Value="16"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Height" Value="40"/>
</Style>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="LightBlue"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding PRO}" Header="Product number" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="10*" />
<DataGridTextColumn Binding="{Binding TotalAmount, StringFormat=N2}" Header="Ukupno" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="10*"/>
<DataGridTextColumn Binding="{Binding PaymentType}" Header="Payment($)" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="10*"/>
<DataGridTextColumn Binding="{Binding ClientName}" Header="Client Name" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="10*"/>
</DataGrid.Columns>
</DataGrid>
Setting the ScrollViewer.CanContentScroll
attached property to false will disable the UI virtualization. Don't do this.
You should probably also consider calling the ProductsController.GetAll()
method on a background thread once the window has been loaded.
You could display a ProgressBar
during the time it takes for the method to complete, eg:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += Window_Loaded;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
pb.Visibility = Visibility.Visible;
Task.Factory.StartNew(()=>
{
return ProductsController.GetAll();
}).ContinueWith(task =>
{
dtgProducts.ItemsSource = task.Result;
pb.Visibility = Visibility.Collapsed;
}, System.Threading.CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
}
}
<Grid>
<DataGrid Grid.Row="1" IsReadOnly="True" Name="dtgProductsts"
MaxWidth="4000" MaxHeight="2000"
HorizontalGridLinesBrush="#d3d3d3" AlternatingRowBackground="#E0E4E5" AlternationCount="2"
GridLinesVisibility="Horizontal" FontSize="16" RowHeight="30" SelectionUnit="FullRow" Background="White" Margin="5,0"
AutoGenerateColumns="False" RowHeaderWidth="0" VerticalGridLinesBrush="#0091EA" CanUserAddRows="False">
<DataGrid.CellStyle>
<StaticResource ResourceKey="DataGridCentering"/>
</DataGrid.CellStyle>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="#0091EA"/>
<Setter Property="Opacity" Value="1"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="FontSize" Value="16"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Height" Value="40"/>
</Style>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding PRO}" Header="Product number" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="10*" />
<DataGridTextColumn Binding="{Binding TotalAmount, StringFormat=N2}" Header="Ukupno" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="10*"/>
<DataGridTextColumn Binding="{Binding PaymentType}" Header="Payment($)" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="10*"/>
<DataGridTextColumn Binding="{Binding ClientName}" Header="Client Name" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="10*"/>
</DataGrid.Columns>
</DataGrid>
<ProgressBar x:Name="pb" IsIndeterminate="True" Visibility="Collapsed" />
</Grid>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.