在频繁更新期间保持DataGridview的性能和定位 [英] Maintain Performance and Positioning of DataGridview during frequent updates

查看:68
本文介绍了在频繁更新期间保持DataGridview的性能和定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview,通常有100行数据(15列 - 小文本< 10个字符,3个数字整数);我正在从一个事件更新它 - 每秒或2发送更新我用来更新DataGridview。我正在使用绑定到List< myowntype>的绑定源。 。



我在更新前获得Scroll位置并在更新后设置它。

即使我这样做 - 滚动条保持在原位(应该如此),但我的DataGridview行在几次更新后(它不应该)转移到开头。滚动位置可能位于最底部,但我的DataGridview从开始行显示。



我也有一个问题是能够使用控件许多更新进入 - 似乎这么小的数据集这不应该是一个问题,但它是 - 我希望更加无缝的用户体验。



1:如何确保DataGridView Rows与Scroll位置保持同步?

2:如何提高用户体验性能还是更新数据?



代码:







I have a datagridview that will typically have 100 rows of data (15 columns - small text < 10 characters, 3 digit integers); I am updating this from an Event - that every second or 2 sends updates which I use to update the DataGridview . I am using a binding source bound to a List<myowntype> .

I am getting the Scroll position before update and setting it after update.
Even though I do this - the scroll bar remains in position (as it should), but my DataGridview Rows are shifted to the beginning after a few updates(which it should not). The scroll position might be at the very bottom, yet my DataGridview is showing from the beginning row.

I also have an issue with being able to use the control with so many updates coming in - seems being so small a data set this should not be an issue, but it is - I would like a more seamless user experience.

1: How to ensure the DataGridView Rows stay in Sync with the Scroll position ?
2: How can I increase the User Experience performance and still update the data ?

CODE:



void UpdateBayGridview(System.Collections.Generic.List<BAY> bay)
{
    int vscroll = dgBinContent.FirstDisplayedScrollingRowIndex;
    int hscroll = dgBinContent.FirstDisplayedScrollingColumnIndex;

    this.BayTable.BeginLoadData();

    DataTable dtupdates = bay.ToDataTable<BAY>();
    DataColumn[] pkUPdateColumn = { dtupdates.Columns["ID"] };
    dtupdates.PrimaryKey = pkUPdateColumn;

    DataColumn[] pkColumn = { this.BayTable.Columns["ID"] };
    this.BayTable.PrimaryKey = pkColumn;
    this.BayTable.Merge(dtupdates);
    this.BayTable.EndLoadData();
    schemachange = false;

    bsBays.ResetBindings(schemachange);

    // Set the scroll positions
    if (vscroll < 0)
        vscroll = 0;

    if (hscroll < 0)
        hscroll = 0;

    dgBinContent.FirstDisplayedScrollingRowIndex = vscroll;
    dgBinContent.FirstDisplayedScrollingColumnIndex = hscroll;

}

推荐答案

由于你没有计时器,你必须保护你反对在完成实际运行之前再次运行的代码。



声明一个在调用例程之间保持的全局变量。

然后更改代码这样。

Since you don't own the timer, you must guard you code against being run again before finishing actual run.

Declare a global variable that persist between calls to the routine.
then change your code this way.
void UpdateBayGridview(System.Collections.Generic.List<bay> bay)
{
    if (updating) return;
    updating = 1;
    int vscroll = dgBinContent.FirstDisplayedScrollingRowIndex;
    int hscroll = dgBinContent.FirstDisplayedScrollingColumnIndex;
 
    this.BayTable.BeginLoadData();
 
    DataTable dtupdates = bay.ToDataTable<bay>();
    DataColumn[] pkUPdateColumn = { dtupdates.Columns["ID"] };
    dtupdates.PrimaryKey = pkUPdateColumn;
 
    DataColumn[] pkColumn = { this.BayTable.Columns["ID"] };
    this.BayTable.PrimaryKey = pkColumn;
    this.BayTable.Merge(dtupdates);
    this.BayTable.EndLoadData();
    schemachange = false;
 
    bsBays.ResetBindings(schemachange);
 
    // Set the scroll positions
    if (vscroll < 0)
        vscroll = 0;
 
    if (hscroll < 0)
        hscroll = 0;
 
    dgBinContent.FirstDisplayedScrollingRowIndex = vscroll;
    dgBinContent.FirstDisplayedScrollingColumnIndex = hscroll;
    updating = 0;
 
}



这可以解决更新时移动光标的问题。



如果在更新例程处于活动状态时移动光标,则也可能存在冲突。


This can solve the problem of moving cursor while updating.

You also have potential conflict if you move the cursor while the update routine is active.


这篇关于在频繁更新期间保持DataGridview的性能和定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆