S
S
skvoshiz2017-03-27 06:01:42
.NET
skvoshiz, 2017-03-27 06:01:42

How to update DataGrid.ItemsSource without freezes (C# WPF .NET 4.5)?

Greetings to all, alas, attempts to find 'bicycles' in Google ended to no avail.
The problem is this:
I have

public static DataTable DataTableAccounts { get; set; }
Which I get from the database.
To display it for the first time in my DataGrid , I do this:
DataGridAccounts.ItemsSource = DataTableAccounts.DefaultView;

Then I start 'manipulating' the DataTableAccounts
and to display the modified version I use a hack:
DataGridAccounts.ItemsSource = null;
DataGridAccounts.ItemsSource = DataTableAccounts.DefaultView;

Everything works fine , BUT I update quite often, about 2 times per second. and DataTableAccounts is large (1000 rows)
Therefore, I have everything in friezes .
The question is:
How to update the DataGrid asynchronously (or with the help of other tricks) without friezes ?
PS I tried also through Binding in the markup, but then nothing is updated =)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vais, 2017-04-02
@vais

Everything works fine, BUT I update quite often, about 2 times per second. and DataTableAccounts is big (1000 rows)

Well, a thousand rows are not immediately visible?
Load first the part that is visible to the user, and when scrolling, load more records
(google - gradual display of data in the datagrid)
Binding in WPF (pulled out of the project, I think you'll figure it out)
<DataGrid x:Name="dg_last_run_params" CanUserAddRows="False" HorizontalScrollBarVisibility="Disabled" AutoGenerateColumns="False" IsReadOnly="True" SelectionMode="Extended"ItemsSource="{Binding}" Margin="5,3,0,0" FontSize="11" BorderBrush="{x:Null}" Grid.Column="1" Grid.Row="1" MouseLeave="dg_last_run_params_MouseLeave" SelectionChanged="dg_last_run_params_SelectionChanged">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Param Name" MinWidth="140" Width="*" Binding="{Binding param_name}" ClipboardContentBinding="{x:Null}" FontSize="10"/>
                            <DataGridTextColumn  Header="Param Value"  MinWidth="170" Width="*" Binding="{Binding param_value}" ClipboardContentBinding="{x:Null}"/>
                        </DataGrid.Columns>

part in C# code
array_workflow_run_param.Clear();
array_workflow_run_param = session.Get_Workflow_Run_Params();
 dg_last_run_params.ItemsSource = array_workflow_run_param;
 dg_last_run_params.Items.Refresh();

internal class WorkflowParametersValue
    {
        public string param_name { get; set; }
        public string param_value { get; set; }
        public string param_man_value { get; set; }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question