DataGridView的虚拟模式,更新行数导致CellValueNeeded解雇所有行 [英] datagridview virtual mode, update RowCount causes CellValueNeeded to fire for all rows

查看:457
本文介绍了DataGridView的虚拟模式,更新行数导致CellValueNeeded解雇所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的DataGridView的虚拟模式,但是当我设置行数为一定数目(以显示滚动条)电网都想拥有的所有行一次,不仅显示。

I'm trying to implement datagridview's virtual mode but when i set RowCount to some number (to show the scrollbar) the grid wants to have all rows at once, not only the displayed.

DataGridView grid = new ...;

grid.VirtualMode = true;
grid.CellValueNeeded += OnCellValueNeeded;
grid.RowCount = dataprovider.GetFullCount();

我怎么能告诉电网只要求显示的行?

How can i tell the grid to only request the rows displayed?

推荐答案

不幸的是,这似乎是标准的行为。我既可以解决由

unfortunatly this seems to be the standard behavior. i could solve it either by

void OnCellValueNeeded(...)
{
    if(!_active) return;
}

grid.VirtualMode = true;
grid.CellValueNeeded += OnCellValueNeeded;
_active = false;
grid.RowCount = dataprovider.GetFullCount();
_active = true;

或实施IBindingList的,ITypedList在后台线程复杂的延迟抓取

or implementing IBindingList, ITypedList with complex lazy fetching in background thread

更新:这个问题似乎已经被修正。它使用了下面的我无法重现:

Update: the problem seems to be fixed now. I can not reproduce it anymore using the following:

static class Program
{
    private static Form form;
    private static int i;

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        var grid = new DataGridView
        {
            Dock = DockStyle.Fill,
            VirtualMode = true,
            AllowUserToAddRows = false,
            Columns =
            {
                new DataGridViewTextBoxColumn { HeaderText = "foo" },
                new DataGridViewTextBoxColumn { HeaderText = "bar" },
            },
        };
        grid.CellValueNeeded += OnCellValueNeeded;
        form = new Form
        {
            Controls = { grid }
        };

        //grid.RowCount = 0;
        grid.RowCount = 10000;

        Application.Run(form);
    }

    private static void OnCellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
    {
        i++;
        form.Text = i.ToString();
        e.Value = "fooValue";
    }
}

这篇关于DataGridView的虚拟模式,更新行数导致CellValueNeeded解雇所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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