0

I need to make DataGridView binded to DataTable that uses information from dictionary. In DatatGridView I need to make next columns: HWID (hidden) | IP | PC Name | User | OS it must update information from dictionary every 3 seconds

That my code:

in config .cs:

public static Dictionary<string, string> Database = new Dictionary<string, string>();
public static DataTable dtComp = new DataTable();

After form loading:

 config.dtComp = new DataTable("dtcomp", "compdt");
        config.dtComp.Columns.Add("HWID");
        config.dtComp.Columns.Add("IP");
        config.dtComp.Columns.Add("PC Name");
        config.dtComp.Columns.Add("User");
        config.dtComp.Columns.Add("OS");



        gridComp.DataSource = config.dtComp;


System.Timers.Timer updateAll = new System.Timers.Timer(3000);
updateAll.Elapsed += updateAll_Elapsed;
updateAll.Start();

in updateAll timer:

 config.dtComp.Clear();


        Dictionary<string, string> dic = new Dictionary<string, string>();
        foreach (var item in config.Database)
        {
            string[] k = item.Key.Split('_');
            if (!dic.ContainsKey(k[0])) dic.Add(k[0], "*");
            dic[k[0]] += item.Value + "*";

        }
        //{[15FGFFF2M6, *127.0.0.1*Username*Windows 7 (x64)*Username_PC*]}
        foreach (var it in dic)
        {
            string[] sp = it.Value.Split('*');
            // 1 = ip, 2 = user, 3 = os, 4 = user
            DataRow r = config.dtComp.NewRow();
            r["HWID"] = it.Key;
            r["IP"] = sp[1];
            r["User"] = sp[2];
            r["OS"] = sp[3];
            r["User"] = sp[4];
            config.dtComp.Rows.Add(r);
        }

that code works but... that code doesn't work... When i'm clickin on tab with DataGridView I had unknown exceptions...

John
  • 1
  • Are you checking that strings contain a "*" before splitting? it.Value.Split('*'); – jdweng Jan 13 '17 at 19:52
  • http://stackoverflow.com/questions/7893773/do-system-timers-timer-run-in-independent-threads and http://stackoverflow.com/questions/2043615/thread-safety-of-a-dictionarytkey-tvalue and http://stackoverflow.com/questions/21310757/thread-safety-for-datatable – Eugene Podskal Jan 13 '17 at 19:56

0 Answers0