DataGridView性能与BindingList数据源结合使用

Arn*_*176 3 c# performance drawing datagridview bindinglist

我正在构建一个必须显示从外部系统接收的数据的应用程序.这些数据可以非常快速地进入,而每行占用的字节数相对较小.这意味着每个时间单元必须添加许多行.我目前正处于这样一个阶段,我看到我收到的数据比我能处理的速度快,这意味着我的内存使用率正在上升.

我认为这很大一部分与绘制实际的dataGridView有关.我对dataGridView做了一些调整,希望它能提高性能.(例如禁用自动尺寸,特殊样式等)

在最近的一次添加中,我添加了行的颜色,这是必需的.目前我的申请表如下:

  1. 我从外部系统接收数据
  2. 我将数据放入队列(ConcurrencyQueue)中
  3. 另一个线程从该队列获取数据,处理它并将其添加到绑定到表的BindingList.

实际添加发生在具有2个参数的函数中:1.包含列(项)项的列表2.行的颜色.(颜色)

它看起来如下(半伪):

/* 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's 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;
}
Run Code Online (Sandbox Code Playgroud)

如上面的代码所示,我收到的颜色被添加到List中,该List在datagridview的事件中使用如下:

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];
}
} 
Run Code Online (Sandbox Code Playgroud)

BindingList定义如下:

BindingList bindingList;

其中ResultRow是一个具有如下结构的类:

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. */
Run Code Online (Sandbox Code Playgroud)

我可以做些相对简单的事情来提高性能吗?我想在处理繁忙时可能会禁用数据网格的绘制,并在完成时绘制.(虽然不是首选)另一件事可能是更少频繁更新,而不是在每个收到的项目之后.(BindingList似乎会在添加内容时自动更新DataGridView)

我希望有人愿意/能够提供帮助.

-编辑-

表单的响应性因为当它以上述方式处理数据时非常糟糕,特别是在一段时间之后.(即使上述过程发生在backgroundworker和后台线程中)

gwi*_*rrr 5

由于网格中的行数很多,性能可能会在一段时间后下降.您应该尝试虚拟模式.

但首先,尝试推迟网格的更新并批量添加新条目,即降低更新频率.因此,在每次批量更新之前:

// stop raising update events
bindingList.RaiseListChangedEvents = false; 
Run Code Online (Sandbox Code Playgroud)

然后:

// restore update events, raise reset event
bindingList.RaiseListChangedEvents = true;
bindingList.ResetBindings() 
Run Code Online (Sandbox Code Playgroud)

在最后一行继续滚动到最后一行.