I wrote a custom control to allow mass selection of rows in a XamDataGrid which runs slowly when there are more than a few hundred rows. It seems the act of setting a record's IsSelected flag takes about one millisecond which adds up for grids with a lot of rows.
foreach (Record r in TheGrid.Records)
The XamDataGrid has a SelectedItems property which has a Records property. If I clear this property the grid updates instantly. The property has an AddRange method that takes an array of Records. AddRange tends to be very fast - Hmmmm.
This code could be cleaner, but it's very fast. I would prefer to populate a List of Records and use the ToArray method.
DataRecord[] records = new DataRecord[TheGrid.Records.Count];
TheGrid.SelectedItems.Records.AddRange(records);
}
Here is some XAML and code that demonstrates the performance difference. Please forgive the non MVVM solution, it's Friday afternoon and I have stuff to do before I go home for the weekend.
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:igDP="http://infragistics.com/DataPresenter"
xmlns:local="clr-namespace:SelectAll"
mc:Ignorable="d"
SizeToContent="WidthAndHeight"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<igDP:XamDataGrid.FieldSettings>
<igDP:FieldSettings AllowEdit="False" CellClickAction="SelectRecord"/>
</igDP:XamDataGrid.FieldSettings>
</igDP:XamDataGrid>
<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal">
<Button Content="Select one at a time" Click="One_Click"/>
<Button Content="Select range" Click="Range_Click"/>
<Button Content="Clear" Click="Clear_Click"/>
</StackPanel>
</Grid>
</Window>
namespace SelectAll
public class cItem
{
public int ID { get; set; }
public partial class MainWindow : Window
{
public List<cItem> Items { get; set; }
PopulateItems();
InitializeComponent();
}
private void PopulateItems()
Items = new List<cItem>();
private void One_Click(object sender, RoutedEventArgs e)
foreach (Record r in TheGrid.Records)
private void Range_Click(object sender, RoutedEventArgs e)
DataRecord[] records = new DataRecord[TheGrid.Records.Count];
TheGrid.SelectedItems.Records.AddRange(records);
}
private void Clear_Click(object sender, RoutedEventArgs e)
TheGrid.SelectedItems.Records.Clear();
}
}
}
No comments:
Post a Comment