ListBox数据源分配

本文关键字:分配 数据源 ListBox | 更新日期: 2023-09-27 18:14:40

我这里有一个带有ListBox的桌面应用程序,它将接受超过10,000条记录(目录及其子目录中的文件)。当我分配它的DataSource时,DataTable可能超过50,000,它使UI挂起,即使它在BackgroundWorkerDoWork内,因此,挂起我的ProgressBar,这表明ListBox中数据分配的进度。

我在这里也使用了这个方法来避免交叉线程,同时分配它的DisplayMemberValueMember,但它仍然会挂起。

代码如下:

private void bgWorkerForLstBox1_DoWork(object sender, DoWorkEventArgs e)
{
    string viewPath = string.Empty;
    if (radFullPath.Checked)
        viewPath = "fullPath";
    else if (radShortPath.Checked)
        viewPath = "truncatedPath";
    else
        viewPath = "fileName";
    if (dt1 != null)
        if (dt1.Rows.Count > 0)
            SetListBox1Props(viewPath, "fullPath");
}
delegate void SetListBox1PropsCallback(string DisplayMember, string ValueMember);
private void SetListBox1Props(string DisplayMember, string ValueMember)
{
    if (this.lstBox1.InvokeRequired)
    {
        SetListBox1PropsCallback d = new SetListBox1PropsCallback(SetListBox1Props);
        this.Invoke(d, new object[] { DisplayMember, ValueMember });
    }
    else
    {
        this.lstBox1.DataSource = dt1;
        this.lstBox1.DisplayMember = DisplayMember;
        this.lstBox1.ValueMember = ValueMember;
    }
}

ListBox数据源分配

要显示的项数对于窗口来说太大了。如果你需要这个,不想实现某种分页,我建议在VirtualMode中使用ListView控件。更多信息请参见此链接:http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.virtualmode.aspx