Take this line:
DataGridView1.DataSource = dt
And move it after all of the For
loops end. This is not the only thing that slows you down, but I suspect it is the biggest... trying to rebuild the grid after every change.
Additionally, you can restructure the loops to filter at each level before calling into the other levels, and save quite a bit of work this way. For example, say you only have 3 lists, each with 5 out of 10 items selected. The original code would run the entire if()
conditions 1000 times, each with 3 boolean comparisons, in order to produce only 125 results. By filtering at each level before descending into the next, you minimize the number of failed checks. The same 3 lists would only require 310 boolean checks to produce the same 125 results if you don't pre-cache selections, or 30 boolean checks + a little extra memory if you do:
Dim dt As New DataTable
dt.Columns.Add("Regi?o", Type.GetType("System.String"))
dt.Columns.Add("Produto", Type.GetType("System.String"))
dt.Columns.Add("Cliente", Type.GetType("System.String"))
dt.Columns.Add("Tipo Atividade", Type.GetType("System.String"))
dt.Columns.Add("Acabamento", Type.GetType("System.String"))
dt.Columns.Add("Cores", Type.GetType("System.String"))
dt.Columns.Add("Gramagem", Type.GetType("System.String"))
dt.Columns.Add("Tipo Embalagem", Type.GetType("System.String"))
dt.Columns.Add("Formatos", Type.GetType("System.String"))
dt.Columns.Add("Contagem", Type.GetType("System.String"))
dt.Columns.Add("Transporte", Type.GetType("System.String"))
dt.Columns.Add("Moeda", Type.GetType("System.String"))
dt.Columns.Add("Pre?o", Type.GetType("System.String"))
dt.Columns.Add("Data Inicio", Type.GetType("System.String"))
dt.Columns.Add("Data Fim", Type.GetType("System.String"))
dt.Columns.Add("Standard", GetType(Boolean))
Dim dataInicio As String = DateTimeInicio.Value.ToString("dd-MM-yyyy")
Dim dataFim As String = DateTimeFim.Value.ToString("dd-MM-yyyy")
'Uncached:
For Each a In ChkListRegiao.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each b In ChkListProduto.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each c In ChkListTipoAtividade.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each d In ChkListAcabamento.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each f In ChkListCores.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each g In ChkListGramagem.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each h In ChkListTipoEmbalagem.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each i In ChkListFormatos.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each j In ChkListContagem.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each k In ChkListTransporte.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
dt.Rows.Add(a, b, "", c, d, f, g, h, i, j, k, txtMoeda.Text, txtPrecoTon.Text, dataInicio, dataFim, chkStandard.Checked)
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
DataGridView1.DataSource = dt
'Cached:
Dim regiao = ChkListRegiao.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim produto = ChkListProduto.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim tipoatividade = ChkListTipoAtividade.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim acabamento = ChkListAcabamento.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim cores = ChkListCores.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim gramagem = ChkListGramagem.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim tipoembalagem = ChkListTipoEmbalagem.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim formatos = ChkListFormatos.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim contagem = ChkListContagem.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim transporte = ChkListTransporte.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
For Each a In regiao
For Each b In produto
For Each c In tipoatividade
For Each d In acabamento
For Each f In cores
For Each g In gramagem
For Each h In tipoembalagem
For Each i In formatos
For Each j In contagem
For Each k In transporte
dt.Rows.Add(a, b, "", c, d, f, g, h, i, j, k, txtMoeda.Text, txtPrecoTon.Text, dataInicio, dataFim, chkStandard.Checked)
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
DataGridView1.DataSource = dt
You'll want to try both to see which is faster for your environment.
But even this seems wrong. Do you really want every possible combination of items selected from all the lists?