0

I have here a desktop application with ListBox that will accept records (files inside a directory and its sub-directories) of more than 10,000. When I assign its DataSource with that DataTable of more than maybe 50,000 it makes the UI hang even though it is inside the DoWork of a BackgroundWorker, thus, hangs also my ProgressBar that indicates the progress of the assignment of data in the ListBox.

I have also used the method here to avoid cross threading while assigning its DisplayMember and ValueMember but still it gets hang.

Here's the code:

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;
    }
}
Community
  • 1
  • 1
Jon P
  • 826
  • 1
  • 7
  • 20
  • 2
    Do you really want to show `50,000` records, can't you implement paging ? – Habib May 22 '13 at 06:08
  • This is a desktop application. If that would help, how could I implement paging like in a `GridView`? – Jon P May 22 '13 at 06:30
  • why did you go for listbox control, binding such amount of data will slow down the application. So use paging concept also use Gridview / Repeater also implement the paging concept in database side. Which database are you using? – Prince Antony G May 22 '13 at 06:36
  • @PrinceAntonyG Again, this is a desktop application not web. I have no database. The function of the program is to list files inside a directory, other details for this would be broad. I just need the way to do it either in `ListBox` or `DataGridView`. Thanks anyway. – Jon P May 22 '13 at 06:49

1 Answers1

1

The number of items you want to show is too large for windows. If you need this and don't want to implement some sort of pagination, i would suggest to use a ListView control in VirtualMode. See this link for more information: http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.virtualmode.aspx

Raimond Kuipers
  • 1,146
  • 10
  • 18
  • I've found something useful here http://www.vbaccelerator.com/home/NET/Code/Controls/ListBox_and_ComboBox/VListBox/VListBox.asp but it just have kind of a sort of bugs... – Jon P May 23 '13 at 01:37