I have created a component to fetch data from a web service. The web service returns an ADO.NET dataset.
I would like this component to be a data source for binding with other controls. The data to source would be the dataset.
So, I added a property DataSet to the component.
private DataSet _dataSet;
public DataSet DataSet
{
get { return _dataSet; }
set
{
if (_dataSet == value)
return;
_dataSet = value;
OnPropertyChanged (this, new PropertyChangedEventArgs ("DataSet"));
}
}
I implemented IListSource for the component.
public partial class MyComponent : Component, INotifyPropertyChanged, IListSource
{
...
IList IListSource.GetList()
{
return DataSet == null ? null : ((IListSource)DataSet).GetList();
}
bool IListSource.ContainsListCollection
{
get { return DataSet != null && ((IListSource)DataSet).ContainsListCollection; }
}
...
}
In the method where the data is received from the web service I do the following:
...
DataSet = response.DataSet; // Copy dataset from web service to the component.
...
I then created a new WinForms project with a form. On this form, I added my component and a DataGridView. Setup the binding between them.
Upon running the application, the web service is called and the dataset is received. However, the UI does not show the data.
I even tried to create a dataset in the component’s constructor and instead of replacing the dataset in the DataSet property I merely do
_dataSet.Reset ();
_dataSet.Merge (value);
Still no effect.
Is there an event or method I need to call to have the dataset communicates it was updated?
What am I missing here?
Yes, there is an event you need! The DataGridView responds by automatically updating its contents against the ListChanged event of the underlying DataSource (which you can raise by implementing IBindingList). IList does not support this event, and without it the DataGridView will not show the newly populated data unless you reassign the DataSource property of the DataGridView to your underyling component each time it changes which you don’t want to do if you will have changes often (it’s expensive).
I had to learn this painful lesson myself here:
Updating DGV IList
Let me know if I’m way off-base here, but this feels like where your problem could be.
EDIT: An example of how this might work –