DataGridView Optimization

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zack00000
    New Member
    • Sep 2008
    • 5

    DataGridView Optimization

    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 programmaticall y

    Code:
    private List<MyObject> dg1Data = new List<MyObject>();
    // ...
    private void UpdateDGV1() {
      if (this.InvokeRequired) {
        this.BeginInvoke((Action)delegate { UpdateDGV1(); });
        return;
      }
    
      int iRowHeight = 15;
      if (dg1Data.Count != xDataGrid1.Rows.Count) xDataGrid1.Rows.Clear();
      for (int i = 0; i < dg1Data.Count; i++) {
        if (i >= xDataGrid1.Rows.Count) {
          xDataGrid1.Rows.Add();
          xDataGrid1.Rows[i].Height = iRowHeight;
        }
        xDataGrid1.Rows[i].Cells[0].Value = dg1Data[i].Value1;
        xDataGrid1.Rows[i].Cells[0].Style.BackColor = Color.Black;
        xDataGrid1.Rows[i].Cells[0].Style.ForeColor = Color.Red;
        xDataGrid1.Rows[i].Cells[1].Value = dg1Data[i].Value2;
        xDataGrid1.Rows[i].Cells[1].Style.BackColor = Color.Black;
        xDataGrid1.Rows[i].Cells[1].Style.ForeColor = Color.Green;
        xDataGrid1.Rows[i].Cells[2].Value = dg1Data[i].Value3;
        xDataGrid1.Rows[i].Cells[2].Style.BackColor = Color.Black;
        xDataGrid1.Rows[i].Cells[2].Style.ForeColor = Color.Blue;
      }
    }
    2. Using BindingSource

    Code:
    private List<MyObject> dg2Data = new List<MyObject>();
    private BindingSource dgv2Binder = new BindingSource();
    // ...
    public Form1() {
      InitializeComponent();
    
      dgv2Binder.DataSource = dg2Data;
      xDataGrid2.AutoGenerateColumns = false;
      xDataGrid2.DataSource = dgv2Binder;
      xDataGrid2.Columns[0].DataPropertyName = "MyValue1";
      xDataGrid2.Columns[1].DataPropertyName = "MyValue2";
      xDataGrid2.Columns[2].DataPropertyName = "MyValue3";
    }
    // ...
    private void UpdateDGV2() {
      if (this.InvokeRequired) {
        this.BeginInvoke((Action)delegate { UpdateDGV2(); });
        return;
      }
    
      dgv2Binder.ResetBindings(false);
    }
    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.
  • Zack00000
    New Member
    • Sep 2008
    • 5

    #2
    Hi,

    Can someone help me please?

    Thanks

    Comment

    Working...