code_junkie

Changing DataGridViewRow Properties in Another Thread

Nov 12th, 2011
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. for (int i = 0; i < dgItemMaster.Rows.Count; i++)
  2. {
  3. if (dgItemMaster.Rows[i].DataBoundItem.GetType().Name == "Package")
  4. {
  5. dgItemMaster.Rows[i].DefaultCellStyle.BackColor = Color.PowderBlue;
  6. }
  7. }
  8.  
  9. void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  10. {
  11. dgItemMaster.DataSource = products;
  12. dgItemMaster.BeginInvoke((Action)(() =>
  13. {
  14. for (int i = 0; i < dgItemMaster.Rows.Count; i++)
  15. {
  16. if (dgItemMaster.Rows[i].DataBoundItem.GetType().Name == "Package")
  17. {
  18. dgItemMaster.Rows[i].DefaultCellStyle.BackColor = Color.PowderBlue;
  19. }
  20. else if (dgItemMaster.Rows[i].DataBoundItem.GetType().Name == "PackageKit")
  21. {
  22. dgItemMaster.Rows[i].DefaultCellStyle.BackColor = Color.Pink;
  23. }
  24. }
  25. }));
  26. }
  27.  
  28. int startIndex = 0;
  29. Action action = null;
  30. action = () =>
  31. { // only processes a batch of 50 rows, then calls BeginInvoke
  32. // to schedule the next batch
  33. int endIndex = startIndex + 50;
  34. if (endIndex > dgItemMaster.Rows.Count) endIndex = dgItemMaster.Rows.Count;
  35.  
  36. if (startIndex > endIndex)
  37. {
  38. for (int i = startIndex; i < endIndex; i++)
  39. {
  40. // process row i
  41. }
  42.  
  43. startIndex = endIndex;
  44. this.BeginInvoke(action); // next iteration
  45. }
  46. };
  47.  
  48. // kick it off
  49. this.BeginInvoke(action);
Add Comment
Please, Sign In to add comment