Hello,
We're working on a project to pull data from servers. The code goes over the network to each server of interest and retrieves some server information (i.e. name, Operating System, etc.) which in turn is added to the ObservableCollection. Everything works fine; however, it can take several minutes to go to each server and pull back the data. Therefore, we would like to populate the gridview as the data is returned. For example...application retrieves the name of server 1, the gridview displays server 1 data while in the background server 2 data is retrieved and displayed once it returns, etc.
So here is what we've tried with Gridview bound to an ObservableCollection.
Retrieving the data with a backgroundworker (which works, but the data still does not display on the grid as retrieved).
Tried other Data Types (DataSet, List, etc.) - they all work but all of the data has to be retrieved before being displayed.
Looked at the OnCollectionChanged event and as each server is queried and populated into the ObservableCollection - the event it fired.
Tried changing the DataLoadMode to Asynchronous
Below is the code behind...
Not much to the XAML but here it is...
We're working on a project to pull data from servers. The code goes over the network to each server of interest and retrieves some server information (i.e. name, Operating System, etc.) which in turn is added to the ObservableCollection. Everything works fine; however, it can take several minutes to go to each server and pull back the data. Therefore, we would like to populate the gridview as the data is returned. For example...application retrieves the name of server 1, the gridview displays server 1 data while in the background server 2 data is retrieved and displayed once it returns, etc.
So here is what we've tried with Gridview bound to an ObservableCollection.
Retrieving the data with a backgroundworker (which works, but the data still does not display on the grid as retrieved).
Tried other Data Types (DataSet, List, etc.) - they all work but all of the data has to be retrieved before being displayed.
Looked at the OnCollectionChanged event and as each server is queried and populated into the ObservableCollection - the event it fired.
Tried changing the DataLoadMode to Asynchronous
Below is the code behind...
using System; |
using System.Collections.Generic; |
using System.Collections.ObjectModel; |
using System.Collections.Specialized; |
using System.ComponentModel; |
using System.DirectoryServices; |
using System.Linq; |
using System.Text; |
using System.Threading; |
using System.Windows; |
using System.Windows.Controls; |
using System.Windows.Data; |
using System.Windows.Documents; |
using System.Windows.Input; |
using System.Windows.Media; |
using System.Windows.Media.Imaging; |
using System.Windows.Shapes; |
using System.Windows.Threading; |
using Telerik.Windows.Controls; |
namespace TestXmlSerialize123 |
{ |
/// <summary> |
/// Interaction logic for Window2.xaml |
/// </summary> |
public partial class Window2 : Window |
{ |
public Window2() |
{ |
InitializeComponent(); |
worker.DoWork += new DoWorkEventHandler(worker_DoWork); |
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); |
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged); |
//worker.WorkerReportsProgress = true; |
//this.radGridView2.IsBusy = true; |
DomainControllerList.CollectionChanged += OnCollectionChanged; |
} |
#region Variables |
BackgroundWorker worker = new BackgroundWorker(); |
#endregion |
private void worker_DoWork(object sender, DoWorkEventArgs e) |
{ |
//Thread.Sleep(5000); |
FetchDomainControllers(); |
} |
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) |
{ |
this.radGridView2.ItemsSource = e.UserState; |
} |
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) |
{ |
this.radGridView2.ItemsSource = DomainControllerList; |
} |
private ObservableCollection<DADdomainControllers> _domainControllerList = new ObservableCollection<DADdomainControllers>(); |
public ObservableCollection<DADdomainControllers> DomainControllerList |
{ |
get { return _domainControllerList; } |
} |
private void FetchDomainControllers() |
{ |
DirectoryEntry rootDSE = new DirectoryEntry("LDAP://rootDSE"); |
string configurationNamingContext = (string)rootDSE.Properties["configurationNamingContext"].Value; |
DirectoryEntry deConfig = new DirectoryEntry("LDAP://" + configurationNamingContext); |
DirectorySearcher dsConfig = new DirectorySearcher(deConfig); |
dsConfig.Filter = "(objectClass=ntDSDSA)"; |
SearchResultCollection results = dsConfig.FindAll(); |
foreach (SearchResult Result in results) |
{ |
DirectoryEntry deDomain = Result.GetDirectoryEntry(); |
if (deDomain != null) |
{ |
string _dnsHostName = deDomain.Parent.Properties["DNSHostName"].Value.ToString(); |
string _serverReference = deDomain.Parent.Properties["serverReference"].Value.ToString(); |
_domainControllerList.Add(new DADdomainControllers { dnsHostName = _dnsHostName, distinguishedName = _serverReference }); |
//radGridView2.Dispatcher.Invoke(DispatcherPriority.Normal,(Action)(() => { radGridView2.ItemsSource = DomainControllerList; })); |
//this.radGridView2.ItemsSource = DomainControllerList; |
} |
} |
} |
public class DADdomainControllers |
{ |
public string dnsHostName {get; set;} |
public string distinguishedName {get; set;} |
} |
private void Window_Loaded(object sender, RoutedEventArgs e) |
{ |
} |
private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
{ |
//MessageBox.Show("Collection Changed"); |
} |
private void radGridView2_Loaded(object sender, RoutedEventArgs e) |
{ |
Thread.Sleep(3000); |
worker.RunWorkerAsync(); |
} |
} |
} |
Not much to the XAML but here it is...
<Window x:Class="TestXmlSerialize123.Window2" |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" |
Title="Window2" Height="600" Width="800" Loaded="Window_Loaded"> |
<Grid> |
<telerik:RadGridView Name="radGridView2" DataLoadMode="Asynchronous" Loaded="radGridView2_Loaded" /> |
</Grid> |
</Window> |