This is a migrated thread and some comments may be shown as answers.
Filling GridView with Multiple Threads
1 Answer 28 Views
This is a migrated thread and some comments may be shown as answers.
Erfan
Top achievements
Rank 1
Erfan asked on 16 Jan 2016, 06:20 AM

I'm going to fill my RadGridView by using multiple threads. My code is as follows but is not working. How can I do this?

BackgroundWorker[] workers = new BackgroundWorker[2];
 
        private void frmTest_Load(object sender, EventArgs e)
        {
            for (int col = 0; col < 300; col++)
                grid.Columns.Add("Col " + col.ToString());
 
            for (int r = 0; r < 2; r++)
            {
                workers[r] = new BackgroundWorker();
                workers[r].DoWork += new DoWorkEventHandler(Worker_DoWork);
                workers[r].RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);
                workers[r].RunWorkerAsync(grid.MasterView);
            }
        }
 
        void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            var rnd = new Random();
            var info = (GridViewInfo)e.Argument;
            var row = new GridViewDataRowInfo(info);
            int i = 0;
            foreach (GridViewCellInfo cell in row.Cells)
            {
                cell.Value = i++;
 
                cell.Style.NumberOfColors = 1;
                cell.Style.BackColor = Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255));
                cell.Style.CustomizeFill = true;
            }
 
            e.Result = row;
        }
 
        void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            grid.Rows.Add((GridViewDataRowInfo)e.Result);
        }

1 Answer, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Sr.
Telerik team
answered on 18 Jan 2016, 03:44 PM
Hello ,

Thank you for writing.

Note that all UI controls safe controls in the whole Windows Forms platform (not just Telerik controls, but all controls out there). Here is an article on MSDN, describing how to make thread-safe Winforms UI application. This means that any control from the Telerik UI for WinForms suite safe as well and cannot be used outside the main UI thread. You should use an Invoke to update the controls in threading scenario.

I hope this information helps. Should you have further questions I would be glad to help.
 
Regards,
Dess
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Asked by
Erfan
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Sr.
Telerik team
Share this question
or