DataViewGrid synchronization C#

120 views Asked by At

I am new in C# and have a requirement where a DataViewGrid have fixed rows and three columns. Each column’s data is coming from different source and three threads are running simultaneously to receive it. Now, is it possible to update these columns i.e. Column one by thread one Column two by thread two Column three by thread three Without using any synchronization object? If this is not possible then I will create three DataViewGrid and which will be updated by individual threads.

2

There are 2 answers

0
user743414 On

Works for me, but I don't like it.

private void button1_Click(object sender, EventArgs e)
{
  for(int i = 0; i < 10; i++)
  {
    DataGridViewRow newRow = new DataGridViewRow();
    this.dataGridView1.Rows.Add(newRow);
  }

  FillThread[] t = new FillThread[3];

  for (int i = 0; i < 3; i++)
  {
    t[i] = new FillThread(this.dataGridView1, i);
  }
}


class FillThread
{
    struct param_t
    {
        public DataGridView TheGrid;
        public int Type;
    }

    private Thread _worker;




    public FillThread(DataGridView theGrid, int type)
    {
        _worker = new Thread(ThreadProc);

        param_t param = new param_t();
        param.TheGrid = theGrid;
        param.Type = type;

        _worker.Start(param);
    }

    public void ThreadProc(object data)
    {
        param_t param = (param_t)data;

        for(int i = 0; i < 10; i++)
        {
            param.TheGrid.Rows[i].Cells[param.Type].Value = i;
        }



    }
}
7
RononDex On

Do you use Windows Forms or WPF? In general you can use something called Bindings. In WinForms there normally is a property "DataContext" on the UI-controls. You can update that property from other threads. You can then control which column is matched to which property of the List or array you provided to the DataContext. In WPF there are observable collection. You also map the control to a Property and then update that property from one of your threads.

It is hard to give you more advice because I am lacking information on which technology you are using and which architecture (MVC, MVVM, ...). If you need more help please update your question and provide us with more information.