By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

DataGridView Optimization

P: 5
Hi,

Is there a way to optimize the DataGridView in terms of drawing speed? The application that I'm building will need to use multiple datagridview controls which will get updated around every 100ms. A receiver will parse the data then pass it to the datagridview, but there is no way to know how many rows will be returned.

Right now I'm testing using 2 methods:
1. Generate the rows programmatically

Expand|Select|Wrap|Line Numbers
  1. private List<MyObject> dg1Data = new List<MyObject>();
  2. // ...
  3. private void UpdateDGV1() {
  4.   if (this.InvokeRequired) {
  5.     this.BeginInvoke((Action)delegate { UpdateDGV1(); });
  6.     return;
  7.   }
  8.  
  9.   int iRowHeight = 15;
  10.   if (dg1Data.Count != xDataGrid1.Rows.Count) xDataGrid1.Rows.Clear();
  11.   for (int i = 0; i < dg1Data.Count; i++) {
  12.     if (i >= xDataGrid1.Rows.Count) {
  13.       xDataGrid1.Rows.Add();
  14.       xDataGrid1.Rows[i].Height = iRowHeight;
  15.     }
  16.     xDataGrid1.Rows[i].Cells[0].Value = dg1Data[i].Value1;
  17.     xDataGrid1.Rows[i].Cells[0].Style.BackColor = Color.Black;
  18.     xDataGrid1.Rows[i].Cells[0].Style.ForeColor = Color.Red;
  19.     xDataGrid1.Rows[i].Cells[1].Value = dg1Data[i].Value2;
  20.     xDataGrid1.Rows[i].Cells[1].Style.BackColor = Color.Black;
  21.     xDataGrid1.Rows[i].Cells[1].Style.ForeColor = Color.Green;
  22.     xDataGrid1.Rows[i].Cells[2].Value = dg1Data[i].Value3;
  23.     xDataGrid1.Rows[i].Cells[2].Style.BackColor = Color.Black;
  24.     xDataGrid1.Rows[i].Cells[2].Style.ForeColor = Color.Blue;
  25.   }
  26. }
2. Using BindingSource

Expand|Select|Wrap|Line Numbers
  1. private List<MyObject> dg2Data = new List<MyObject>();
  2. private BindingSource dgv2Binder = new BindingSource();
  3. // ...
  4. public Form1() {
  5.   InitializeComponent();
  6.  
  7.   dgv2Binder.DataSource = dg2Data;
  8.   xDataGrid2.AutoGenerateColumns = false;
  9.   xDataGrid2.DataSource = dgv2Binder;
  10.   xDataGrid2.Columns[0].DataPropertyName = "MyValue1";
  11.   xDataGrid2.Columns[1].DataPropertyName = "MyValue2";
  12.   xDataGrid2.Columns[2].DataPropertyName = "MyValue3";
  13. }
  14. // ...
  15. private void UpdateDGV2() {
  16.   if (this.InvokeRequired) {
  17.     this.BeginInvoke((Action)delegate { UpdateDGV2(); });
  18.     return;
  19.   }
  20.  
  21.   dgv2Binder.ResetBindings(false);
  22. }
When I measures the time taken for each method to perform datagridview's OnPaint function, the first method somehow significantly faster (on average 20% faster).

So my first question is; did I do something wrong on updating the datasource? (because i thought using datasource is suppossed to be better?)

Then my second question, is there a way to improve the speed, or is there a better/alternative form control to use?

Thanks in advance.
Feb 18 '09 #1
Share this Question
Share on Google+
1 Reply


P: 5
Hi,

Can someone help me please?

Thanks
Feb 23 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.