DataGridView performance combined with BindingList data source

I am creating an application that should display data received from an external system. This data can be very fast, and the number of bytes that each row occupies is relatively small. This means that you need to add a lot of lines per unit of time. I am now at a point where I seem to be getting data faster than I can handle as my memory usage grows.

I think most of this is related to drawing the actual dataGridView. I made small changes to the dataGridView in the hope that this will increase performance already. (e.g. disable auto-size, special styles, etc.)

In a recent addition, I added line coloring, which was necessary. Currently my application works as follows:

  • I get data from an external system
  • I queue data (ConcurrencyQueue) by thread
  • Another thread receives data from this queue, processes it, and adds it to the BindingList bound to the table.

Actual addition takes place in a function that has 2 parameters: 1. A list containing elements for columns (elements) 2. Color for a row. (Colour)

looks like this (semi-pseudo):

/* Store the color for the row in the color list so it is accessible from the event */  

rowColors.Add(rowColor);    //Class variable that stored the colors of the rows used in the DataGridCellFormatting event

/* Create the row that is to be added. */
ResultRow resultRow = new ResultRow();

foreach(item in items)
{
    resultRow.Set(item); /* It actually a dictionary because some fields are optional, hence this instead of a     direct constructor call) */
}

bindingList.Add(resultRow);

/* Row coloring based on error is done in the OnCellFormatting() */


/* Auto scroll down */
if (dataGrid.Rows.Count > 0)
{
    dataGrid.FirstDisplayedScrollingRowIndex = dataGrid.Rows.Count - 1;
}

As the code above shows, the color that I get is added to the list that is used in the datagridview event as follows:

void DataGridCellFormattingEvent(object sender, DataGridViewCellFormattingEventArgs e)
{
    // done by column so it happens once per row
    if (e.ColumnIndex == dataGrid.Columns["Errors"].Index)
    {
        dataGrid.Rows[e.RowIndex].DefaultCellStyle.BackColor = rowColors[e.RowIndex];
}
} 

BindingList is defined as follows:

BindingList bindingList;

where ResultRow is a class with a structure like this:

public class ResultRow
{
    private int first = 0;
    private string second = "";
    private UInt64 third = 0;
    private IPAddress fourth = null;
    //etc

    public ResultRow()
    {
    }

    public void Set (<the values>) //In actuallity a KeyValuePair
    {
        //field gets set here
    }

    public UInt64 Third
    {
        get { return third; }
        set { third = value; }
    }

    /* etc. */

, ? datagrid, , . ( ). , , , . (BindingList, , DataGridView, - , )

, - / .

-edit -

, , , , . ( )

+3
2

, , , .

- - datagrid. datagrid winforms, . , 20 ( ), 20 datagrid . , , datagrid . ( CellValueNeeded ), , , bindingsource. . http://msdn.microsoft.com/en-us/library/ms171622.aspx.

, , , "" . , bindlist , . , , (, ), . , , - , , . . Control.SuspendLayout() Control.ResumeLayout().

, , .

0

- . .

, .. . , :

// stop raising update events
bindingList.RaiseListChangedEvents = false; 

:

// restore update events, raise reset event
bindingList.RaiseListChangedEvents = true;
bindingList.ResetBindings() 

, .

+4

All Articles