Dictionary as DataSource for GridGroupingControl

Hi there,

I am doing something wrong and hope you can help. Basically i am trying to use a dictionary as the datasource for a gridGroupingControl. I have several code attempts but each has problems.

How do I do this correctly? 


Below are some details of my situation.
Note that i will have a mass of data and so cannot reload the entire grid each time a value changes in the dictionary's data. I need to only update the cells/Visible range that actually changed.

I need to display a mass of data on a grid (12k rows). The only thing that will happen on the grid is sort, grouping and cell background updates as the data changes (Blinking)
However no data will be edited on the grid itself. grid.readonly=true
All data manipulation will be on the original data  itself. This includes Add, Edit, and removal of dictionary entries.

My data is stored in a dictionary as follows
class InstrumentDirectoryEntry
{
uint ID;
string Name;
int closingPrice;
//etc.
}

with the dictionary defined as follows:
 public static Dictionary<uint, InstrumentDirectoryEntry> Instruments;




Ideally running the below code should auto update the visible grid:

App.Instruments[19081].ClosingPrice = 99;  //Editing existing item
App.Instruments.Remove(19082);         //Removing item
App.Instruments.Add(19080, newIns);          //Adding new instr  



4 Replies

DA David November 30, 2017 06:42 PM UTC

I don't believe there's a BindableDictionary in C#. Generally you use a BindingList.

I assume the reason you want to use a dictionary is for fast lookups on a key? What I've done in the past was keep a sortedlist and then create a binding list based off of my sortedlist.values.

grid.DataSource = mydictionary.Values.ToList<T>();


AR Arulpriya Ramalingam Syncfusion Team December 2, 2017 04:39 AM UTC

Hi James, 
 
Thanks for contacting Syncfusion support. 
 
As per our current architecture, the GridGroupingControl does not have the support to set a Dictionary collection as datasource. The grid supports the data sources in the following link DataSources for grid . By default, the records will be updated whenever the changes are performed in the underlying data source. To achieve your scenario, we would suggest you to set the values of Dictionary collection as BindingList and implement the INotifyPropertyChanged to reflect the changes in the grid. We have created a simple sample as per your requirement. Please make use of below code and sample, 
 
Code example 
 
dataSource = new Dictionary<int, BindingList<Data>>(); 
for (int i = 0; i < 10; i++) 
{ 
    BindingList<Data> data = new BindingList<Data>(); 
    if (!dataSource.ContainsKey(i)) 
    { 
        for (int j = 0; j < 10; j++) 
        { 
            data.Add(new Data(j.ToString(), "Cat" + j.ToString(), "Desc" + j.ToString(), "sample" + j.ToString())); 
        } 
        dataSource.Add(i, data); 
    } 
} 
//To set the datasource 
this.gridGroupingControl1.DataSource = dataSource[0]; 
//Event customization 
private void add_Click(object sender, EventArgs e) 
{ 
    Random rand = new Random(); 
    string r = rand.Next(100).ToString(); 
    if (this.gridGroupingControl1.DataSource is BindingList<Data>) 
    { 
        BindingList<Data> dataCollection = this.gridGroupingControl1.DataSource as BindingList<Data>; 
        //to add the data 
        dataCollection.Add(new Data(r, "Cat" + r, "Desc" + r, "Sample" + r)); 
    } 
} 
//Event customization 
private void remove_Click(object sender, EventArgs e) 
{ 
    if (this.gridGroupingControl1.DataSource is BindingList<Data>) 
    { 
        BindingList<Data> dataCollection = this.gridGroupingControl1.DataSource as BindingList<Data>; 
        //Remove data from the collection 
        dataCollection.RemoveAt(0); 
    } 
} 
 
 
 
Please let us know if you have any concerns. 
 
Regards, 
Arulpriya 



JR James Roodt December 8, 2017 09:22 AM UTC

Hi Arulpriya,

Due to other issues I have been forced to save my incoming data in a DataTable which negates this issue and simplifies grids in general.

But thank you for your help.


MG Mohanraj Gunasekaran Syncfusion Team December 11, 2017 05:40 AM UTC

Hi James, 
 
Thanks for your update. 
 
We are happy to hear that your reported problem has resolved. 
 
Please let us know if you have any concerns. 
 
Regards, 
Mohanraj G 


Loader.
Up arrow icon