Here is a somewhat hacked additional version that shows how to load a DataGrid when binding to a DataView using still BeginInvoke.
The code still loads one row at a time into the DataGrid.
You'll need to modify as needed; I am loading from the AdventureWorks sample using the Loaded event.
Here is how the ViewModel works:
- First load the columns using a SQL statement with a Where clause of 1=0
- Call Dispatcher.BeginInvoke to first load a subset of data
- Then call Dispatcher.BeginInvoke again to load the remaining data
Here is the window:
<Window x:Class="DatagridBackgroundWorker.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfToolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
Loaded="Window_Loaded"
Title="Main Window" Height="400" Width="800">
<DockPanel>
<Grid>
<WpfToolkit:DataGrid
Grid.Column="1"
SelectedItem="{Binding Path=SelectedGroup, Mode=TwoWay}"
ItemsSource="{Binding Path=GridData, Mode=OneWay}" >
</WpfToolkit:DataGrid>
</Grid>
</DockPanel>
</Window>
Here is the Window code-behind with the Loaded event:
public partial class MainView : Window
{
ViewModels.MainViewModel _mvm = new MainViewModel();
public MainView()
{
InitializeComponent();
this.DataContext = _mvm;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Dispatcher d = this.Dispatcher;
_mvm.LoadData(d);
}
}
Here the ViewModel:
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
// load the connection string from the configuration files
_connectionString = ConfigurationManager.ConnectionStrings["AdventureWorks"].ConnectionString;
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
conn.Open();
// load no data 1=0, but get the columns...
string query =
"SELECT [BusinessEntityID],[Name],[SalesPersonID],[Demographics],[rowguid],[ModifiedDate] FROM [Sales].[Store] Where 1=0";
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(_ds);
}
}
// only show grid data after button pressed...
private DataSet _ds = new DataSet("MyDataSet");
public DataView GridData
{
get
{
return _ds.Tables[0].DefaultView;
}
}
private void AddRow(SqlDataReader reader)
{
DataRow row = _ds.Tables[0].NewRow();
for (int i = 0; i < reader.FieldCount; i++)
{
row[i] = reader[i];
}
_ds.Tables[0].Rows.Add(row);
}
public void LoadData(Dispatcher dispatcher)
{
// Execute a delegate to load the first number on the UI thread, with a priority of Background.
dispatcher.BeginInvoke(DispatcherPriority.Background, new LoadNumberDelegate(LoadNumber), dispatcher, true, 1);
}
// Declare a delegate to wrap the LoadNumber method
private delegate void LoadNumberDelegate(Dispatcher dispatcher, bool first, int id);
private void LoadNumber(Dispatcher dispatcher, bool first, int id)
{
try
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
conn.Open();
// load first 10 rows...
String query = string.Empty;
if (first)
{
// load first 10 rows
query =
"SELECT TOP 10 [BusinessEntityID],[Name],[SalesPersonID],[Demographics],[rowguid],[ModifiedDate] FROM [AdventureWorks2008].[Sales].[Store] ORDER By [BusinessEntityID]";
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;
int lastId = -1;
SqlDataReader reader = cmd.ExecuteReader();
if (reader != null)
{
if (reader.HasRows)
{
while (reader.Read())
{
lastId = (int)reader["BusinessEntityID"];
AddRow(reader);
}
}
reader.Close();
}
// Load the remaining, by executing this method recursively on
// the dispatcher queue, with a priority of Background.
dispatcher.BeginInvoke(DispatcherPriority.Background,
new LoadNumberDelegate(LoadNumber), dispatcher, false, lastId);
}
else
{
// load the remaining rows...
// SIMULATE DELAY....
Thread.Sleep(5000);
query = string.Format(
"SELECT [BusinessEntityID],[Name],[SalesPersonID],[Demographics],[rowguid],[ModifiedDate] FROM [Sales].[Store] Where [BusinessEntityID] > {0} ORDER By [BusinessEntityID]",
id);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;
SqlDataReader reader = cmd.ExecuteReader();
if (reader != null)
{
if (reader.HasRows)
{
while (reader.Read())
{
AddRow(reader);
}
}
reader.Close();
}
}
}
}
catch (SqlException ex)
{
}
}
private string _connectionString = string.Empty;
public string ConnectionString
{
get { return _connectionString; }
set
{
_connectionString = value;
OnPropertyChanged("ConnectionString");
}
}
}