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.
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;
}