简体   繁体   English

DataViewGrid同步C#

[英]DataViewGrid synchronization C#

I am new in C# and have a requirement where a DataViewGrid have fixed rows and three columns. 我是C#的新手,并且对DataViewGrid具有固定的行和三列的要求。 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 ie Column one by thread one Column two by thread two Column three by thread three Without using any synchronization object? 现在,是否可以更新这些列,即按线程1的第一列,按线程2的第二列,按线程3的第三列而不使用任何同步对象? If this is not possible then I will create three DataViewGrid and which will be updated by individual threads. 如果这不可能,那么我将创建三个DataViewGrid并将由各个线程进行更新。

Do you use Windows Forms or WPF? 您使用Windows窗体还是WPF? In general you can use something called Bindings. 通常,您可以使用称为绑定的东西。 In WinForms there normally is a property " DataContext " on the UI-controls. 在WinForms中,UI控件上通常有一个属性“ DataContext ”。 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. 然后,您可以控制哪一列与您提供给DataContext的List或数组的哪个属性匹配。 In WPF there are observable collection. 在WPF中有可观察的集合。 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, ...). 很难给您更多建议,因为我缺少有关您正在使用哪种技术和哪种体系结构(MVC,MVVM等)的信息。 If you need more help please update your question and provide us with more information. 如果您需要更多帮助,请更新您的问题并向我们提供更多信息。

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



    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM