0

I get the distinct values from a Datatable with the code below. This is very slow. With 9000 unique values I stopped the reading after 2 minutes. Is it possible to do it faster?

private void GetWaarde(string Veldnaam)
        {
            if (DatatabelExport.Rows.Count > 0)
            {
                Cursor.Current = Cursors.WaitCursor;
                DataGridViewComboBoxColumn cb = (DataGridViewComboBoxColumn)dataGridView3.Columns["Waarde"];
                if (cb.Items.ToString() != "")  //als veld Waarde al gevuld is dan niet opnieuw vullen
                {
                    var x = (from r in DatatabelExport.AsEnumerable()
                             select r[dataGridView1.Columns[Veldnaam].Name]).Distinct().ToList();

                    for (int i = 0; i < x.Count - 1; i++)
                    {
                        cb.Items.Add(x[i].ToString());
                    }
                }
                cb.Dispose();
                Cursor.Current = Cursors.Default;
            }
        } 
Cyber Progs
  • 3,656
  • 3
  • 30
  • 39
Hansvb
  • 113
  • 1
  • 1
  • 13
  • First I wouild reconsider the UI design. No user iis going to enjoy being confronted with a CBO with 9000 items. Second work from the DGV's datasource (datatable?). Third, rather than add items one by one, use AddRange or even better use your list as a DataSource. Fourth, which part is taking up the most time? Using similar code I can get 20000 unique values (the ID) from a datatable in 4 or 5 seconds – Ňɏssa Pøngjǣrdenlarp Oct 28 '17 at 18:09
  • The time-consuming part of the code is not getting unique values from data table. It's the part that you are adding items to the combo box. To see the solution take a look at [this post](https://stackoverflow.com/a/39338415/3110834). – Reza Aghaei Oct 28 '17 at 18:42
  • I've noticed that you have not accepted or voted for any of the answers in your previous questions. Please take a [tour] and learn how to accept and upvote answers and take a review on your previous questions and accept and upvote good answers. – Reza Aghaei Oct 28 '17 at 18:46

1 Answers1

0

Firstly using .DataSource convert it to a DataTable; and use this DataTable to extract data.