Adrian
Adrian

Reputation: 1149

IDictionary of class displayed in a table c#

I have an IDictionary which holds an ever changing list of my class and I want to display the collection in a winforms UI probably using DataGridView or another control. I plan to have timer that refreshes the table every 1 to 2 seconds and a pause button. when any given row is clicked I need to be able to get the class from the dictionary or return the first field which be the key in the IDictionary.

So I create the IDictionary thus:

public static IDictionary<string, AClass> aList = new Dictionary<string, AClass>();

AClass is a simple collection of strings:

public class AClass
{
    public string data1{ get; set; }
    public string data2{ get; set; }
}

I add the AClass to the IDictionary thus:

 if (!MainForm.aircraftList.ContainsKey(strMyData))
        {
            MainForm.aList[strMyData] = new AClass{ data1= strMyData};
        }

How can I create the table with all the columns from AClass which are around 12 and rows from the IDictionary aList of which there is a variable number hovering around 100.

Upvotes: 0

Views: 324

Answers (2)

TnTinMn
TnTinMn

Reputation: 11801

Since you are using an IDictionary, the default binding mechanism will not be able to retrieve the individual entries nor the properties on the class AClass. You can create a custom BindingSource to handle these tasks.

The primary responsibility of custom BingSource is to supply a collection of ProperyDescriptors for the AClass type. These are retrieved using the TypeDescriptor.GetProperties Method. The BindingSource also needs to access the underlying DataSource items by index; this is handled in the indexer of the BindingSource.

To use this BindingSource, create an instance of it passing your IDictionary instance and then assign this BindingSource to the DataGridView's DataSource property.

internal class myBindingSource : BindingSource
    {
    private IDictionary<string, AClass> source;
    private System.ComponentModel.PropertyDescriptorCollection props;

    public myBindingSource(IDictionary<string, AClass> source)
        {
        this.source = source;
        props = System.ComponentModel.TypeDescriptor.GetProperties(typeof(AClass));
        this.DataSource = source;
        }

    public override System.ComponentModel.PropertyDescriptorCollection GetItemProperties(System.ComponentModel.PropertyDescriptor[] listAccessors)
        {
        return props;
        }

    public override object this[int index]
        {
        get {return this.source.Values.ElementAtOrDefault(index);}
        set {}
        }

    public override bool AllowNew
        {
        get { return false; }
        set{}
        }

    public override bool AllowEdit
        {
        get { return false; }
        }

    public override bool AllowRemove
        {
        get {return false;}
        }

    public override int Count
        {
        get {return ((this.source == null) ? -1 : this.source.Count);}
        }
    }

Edit: I have added an override of the Count property to the code to allow the the BindingSource.ResetBindings Method to work properly when called to force the bound control to re-read the values from the BindingSource. I have also updated the indexer code.

If you modify the custom BindingSource's underlying DataSource after assigning it as the DataGridView's DataSource, you will need to call the BindingSource.ResetBindings method for those changes to be reflected in the grid.

For Example:

    private IDictionary<string, AClass> aList;
    private myBindingSource bs;

    public Form1()
        {
        InitializeComponent();
        aList = new Dictionary<string, AClass>();

        bs = new myBindingSource(aList);
        dgv.DataSource = bs;

        aList.Add("1", new AClass());
        aList.Add("2", new AClass { data1 = "AA", data2 = "BB" });
        bs.ResetBindings(false);
        }

Upvotes: 0

JohnG
JohnG

Reputation: 9479

To possibly help you get started, below is code that uses a DataGridView to display data using a DataTable; a List<AClass> objects and also using a Dictionary<string, AClass>. As I said using a dictionary, you will have to do some extra steps to get the AClass Value from the dictionary. These extra steps indicate that the Dictionary is not necessarily the best data structure to use as a DataSource if there a multiple variables in your class.

The code below uses a DataGridView and four (4) buttons. There are buttons to display data into the DataGridView using a DataTable; a List<AClass> (added a key), and a Dictionary<string, AClass> and finally a button to clear the DataGridView

Hope this helps.

public Dictionary<string, AClass> DGVDictionary;
public DataTable DGVTable;
public List<AClass> DGVList;

public Form1() {
  InitializeComponent();
}

private Dictionary<string, AClass> GetDictionary() {
  Dictionary<string, AClass> dictionary = new Dictionary<string, AClass>();
  for (int key = 0; key < 15; key++) {
    AClass ac = new AClass();
    ac.data1 = "data1" + key;
    ac.data2 = "data2" + key;
    dictionary.Add(key.ToString(), ac);
  }
  return dictionary;
}

private void CreateTable() {
  DGVTable = new DataTable();
  DGVTable.Columns.Add("key", typeof(int));
  DGVTable.Columns.Add("data1", typeof(string));
  DGVTable.Columns.Add("data2", typeof(string));
}

private void FillDataTable() {
  for (int key = 0; key < 15; key++) {
    AClass ac = new AClass();
    ac.data1 = "data1" + key;
    ac.data2 = "data2" + key;
    DGVTable.Rows.Add(key, ac.data1, ac.data2);
  }
}

private List<AClass> FillList() {
  List<AClass> list = new List<AClass>();
  for (int key = 0; key < 15; key++) {
    AClass ac = new AClass();
    ac.key = key;
    ac.data1 = "data1" + key;
    ac.data2 = "data2" + key;
    list.Add(ac);
  }
  return list;
}

 private void buttonDataTable_Click(object sender, EventArgs e) {
  CreateTable();
  FillDataTable();
  dataGridView1.DataSource = DGVTable;
  MessageBox.Show("DGV with a DataTable");
}

private void buttonList_Click(object sender, EventArgs e) {
  DGVList = FillList();
  dataGridView1.DataSource = DGVList;
  MessageBox.Show("DGV with a List<AClass>");
}

private void buttonDictionary_Click(object sender, EventArgs e) {
  DGVDictionary = GetDictionary();
  dataGridView1.DataSource = DGVDictionary.ToList();
  MessageBox.Show("DGV with a Dictionat<string, AClass>");
}

private void buttonClear_Click(object sender, EventArgs e) {
  dataGridView1.DataSource = null;
}

Upvotes: 1

Related Questions