0

I've got a list of arrays public List<string[]> logList = new List<string[]>(); This is all read in from some custom system log files (tab delimited cells in the file, with each row being a new line) that user picks from an open file dialog.

I'm trying to load all of this into a Datagrid view for observation, but my big issues are that:

1) Column count is variable based on the size of string[X] where X is columns

2) Row count is variable based on the size of the file read in

So my Datagrid view starts out tiny, but can potentially have 1100 rows that are each 253 columns long.

What's happening is that, when I'm trying to populate the Datagrid view with this data the whole GUI is locked up and buttons are inaccessible, Graph isnt updating visually until complete. I attempted using backgroundWorker for this, but because the DataGridView is being manipulated, that throws crossThread exceptions. Below is current, sluggish implementation:

    public List<string[]> logList = new List<string[]>();
    //...
    //...
    private void button_loadFile_Click(object sender, EventArgs e)
    {
        dataGridView1.Columns.Clear();
        logList.Clear();
        int index=0;
        openFileDialog1.ShowDialog();
        index = openFileDialog1.FileName.LastIndexOf("\\");
        if(index>0)
            openFileDialog1.InitialDirectory = (openFileDialog1.FileName.Substring(0,index));
        main();
    }
    //...
    //...
    public void main()
    {
        readInData();
        expandCollumns(findCollumnCount());
        dataGridView1.Rows.Insert(0, logList.Count);
        for (int i = 0; i < logList.Count; i++)
        {
            fillInRow(logList[i], i);
        }
        markRows();
    }
    private void readInData()
    {
        string FileName = openFileDialog1.FileName;
        string[] masterFile = System.IO.File.ReadAllLines(FileName);
        for (int i = 0; i < masterFile.Length; i++)
        {
            string[] splitMaster = masterFile[i].Split('\t');
            logList.Add(splitMaster);
        }
    }
    private int findCollumnCount()
    {
        int maxCollumns = 0;

        for (int i = 0; i < logList.Count; i++)
        {
            if (maxCollumns < logList[i].Length)
            {
                maxCollumns = logList[i].Length;
            }
        }
        return maxCollumns;
    }
    private void expandCollumns(int count)
    {
        for(int i=1;i<=count;i++)
        {
            dataGridView1.ColumnCount = dataGridView1.ColumnCount + 1;
            dataGridView1.Columns[(dataGridView1.ColumnCount)-1].HeaderText= "data_"+ Convert.ToString(i);
        }
    }
    private void fillInRow(string[] logLine, int rowValue)
    {
        for (int j = 0; j < logLine.Length; j++)
        {
            dataGridView1[j, rowValue].Value = logLine[j];
        }
    }
    private void markRows()
    {
        for (int i = 0; i < (dataGridView1.RowCount -1); i++)
        {
            caseSearch(dataGridView1[4, i]);
        }
    }
    private void caseSearch(DataGridViewCell currentCell)
    {
        string text = Convert.ToString(currentCell.FormattedValue);

        if (text.Contains("LoadPort"))              dyeCell(Color.FromArgb(193, 155, 2), currentCell); //dark mustard
        else if (text.Contains("ProcessJobManager")) dyeCell(Color.FromArgb(153,153,153), currentCell); //grey
        else if (text.Contains("Chamber"))         dyeCell(Color.FromArgb(131,240,252), currentCell); //baby blue
        else if (text.Contains("exception"))      dyeCell(Color.FromArgb(255,0,0),     currentCell); //red              
        else                                         dyeCell(Color.White,                 currentCell); //white
    }
    private void dyeCell(Color C, DataGridViewCell currentCell)
    {
        //change the cell colour based on some key word searches
        currentCell.Style.BackColor = C;
        dataGridView1[currentCell.ColumnIndex - 1, currentCell.RowIndex].Style.BackColor = C;
        dataGridView1[currentCell.ColumnIndex + 1, currentCell.RowIndex].Style.BackColor = C;
    }

Ideally I'd have expandCollumns, fillInRow, and markRows not have the GUI hang, but visually I want to see each row pop in, and each cell get marked as it happens (current implementation dataGridView blinks grey until all data processed in and marked)

Medic3000
  • 786
  • 4
  • 20
  • 44
  • Backgroundworker thread is only a step1. Step2 would be to use `this.Invoke(delegate{})` to update UI without blocking of the UI thread. Step3 (if necessary) would be to use concurrent collection to avoid cross- thread conflicts, data mismatch etc. – Fabjan Jan 22 '18 at 15:48
  • I dont know enough about invoke/delagate, pretty fresh to multithreadding in C#. any useful links or advise would be appreciated – Medic3000 Jan 22 '18 at 15:52
  • Some links as requested: [Invoke](https://stackoverflow.com/questions/14703698/invokedelegate) and [Concurrent collections](https://learn.microsoft.com/en-us/dotnet/standard/collections/thread-safe/) – Fabjan Jan 22 '18 at 16:04

0 Answers0