obamaself Posted February 23, 2012 at 02:46 PM Report Share #440839 Posted February 23, 2012 at 02:46 PM Boas, tenho células na minha datagridview vazias. E eu quero ocultar as colunas vazias. Pus uma message box para verificar o valor das vazias e aparece-me este erro: O argumento 'Prompt' não pode ser convertido para o tipo 'String'. o código que estou a usar para ocultar é: For coluna As Integer = 0 To dg.ColumnCount - 1 If coluna > index Then Dim eliminar As Boolean = False For linha As Integer = 0 To dg.RowCount - 1 If dg.Item(coluna, linha).Value Is DBNull.Value Then eliminar = True Else eliminar = False Exit For End If Next If eliminar = True Then dg.Columns(coluna).Visible = False End If End If Next Link to comment Share on other sites More sharing options...
Caça Posted February 23, 2012 at 03:03 PM Report Share #440842 Posted February 23, 2012 at 03:03 PM Onde é que tens a MessageBox? Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
obamaself Posted February 23, 2012 at 03:08 PM Author Report Share #440844 Posted February 23, 2012 at 03:08 PM no evento CellClick da datagridview. Isso era só para ver porque é que não está a ocultar e o valor da celula. pus assim no evento: If (dg.CurrentCell.Value IsNot DBNull.Value) Then MsgBox(dg.CurrentCell.Value) ElseIf dg.CurrentCell.Value Is DBNull.Value Then MsgBox("nulo") End If e mostra que a célula está com valor nulo. Mas não está a ocultar, não sei porquê. Link to comment Share on other sites More sharing options...
Caça Posted February 23, 2012 at 03:10 PM Report Share #440846 Posted February 23, 2012 at 03:10 PM Ok, mas mostra o código, deve ser lá que está o erro. Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
obamaself Posted February 23, 2012 at 03:16 PM Author Report Share #440847 Posted February 23, 2012 at 03:16 PM Para ocultar: For coluna As Integer = 0 To dg.ColumnCount - 1 If coluna > index Then Dim eliminar As Boolean = False For linha As Integer = 0 To dg.RowCount - 1 If dg.Item(coluna, linha).Value Is DBNull.Value Then eliminar = True Else eliminar = False Exit For End If Next If eliminar = True Then dg.Columns(coluna).Visible = False End If End If Next Link to comment Share on other sites More sharing options...
Caça Posted February 23, 2012 at 03:19 PM Report Share #440849 Posted February 23, 2012 at 03:19 PM no evento CellClick da datagridview. Isso era só para ver porque é que não está a ocultar e o valor da celula. pus assim no evento: If (dg.CurrentCell.Value IsNot DBNull.Value) Then MsgBox(dg.CurrentCell.Value) ElseIf dg.CurrentCell.Value Is DBNull.Value Then MsgBox("nulo") End If e mostra que a célula está com valor nulo. Mas não está a ocultar, não sei porquê. Com este código já não dá o erro que indicas-te, certo? Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
obamaself Posted February 23, 2012 at 03:19 PM Author Report Share #440850 Posted February 23, 2012 at 03:19 PM certo. Link to comment Share on other sites More sharing options...
Caça Posted February 23, 2012 at 03:22 PM Report Share #440851 Posted February 23, 2012 at 03:22 PM Podes fazer mais o menos assim Dim PodeOcultar As Boolean For Each C As DataGridViewColumn In Dg.Columns PodeOcultar = False For Each L As DataGridViewRow In Dg.Rows If L.Cells(C.Index).Value Is DBNull.Value Then PodeOcultar = True Exit For End If Next If PodeOcultar Then C.Visible = False End If Next Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
obamaself Posted February 23, 2012 at 03:27 PM Author Report Share #440852 Posted February 23, 2012 at 03:27 PM mesmo assim não dá. Dim PodeOcultar As Boolean For Each C As DataGridViewColumn In dg.Columns PodeOcultar = False For Each L As DataGridViewRow In dg.Rows If L.Cells(C.Index).Value Is DBNull.Value Then PodeOcultar = True Else PodeOcultar = False Exit For End If Next If PodeOcultar Then C.Visible = False End If Next eu quer ocultar as colunas que não tem nenhum valor, se tiverem algum não se oculta Link to comment Share on other sites More sharing options...
Caça Posted February 23, 2012 at 03:29 PM Report Share #440854 Posted February 23, 2012 at 03:29 PM Pois, enganei-me, só tens de fazer o inverso Dim PodeOcultar As Boolean For Each C As DataGridViewColumn In Dg.Columns PodeOcultar = True For Each L As DataGridViewRow In Dg.Rows If L.Cells(C.Index).Value IsNot DBNull.Value Then PodeOcultar = False Exit For End If Next If PodeOcultar Then C.Visible = False End If Next Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
obamaself Posted February 23, 2012 at 03:37 PM Author Report Share #440857 Posted February 23, 2012 at 03:37 PM não dá... 😁 olha vou-te mostrar o código todo que tenho. No evento da checkbox: If colunas_nao_preenchidas.Checked = True Then colunas_preenchidas.Checked = False Ocultar_Colunas_Vazias() ElseIf colunas_nao_preenchidas.Checked = False Then colunas_preenchidas.Checked = True colunas_nao_preenchidas.Checked = False End If o Sub Ocultar_Colunas_Vazias: Dim PodeOcultar As Boolean For Each C As DataGridViewColumn In dg.Columns PodeOcultar = True For Each L As DataGridViewRow In dg.Rows If L.Cells(C.Index).Value IsNot DBNull.Value Then PodeOcultar = False Exit For End If Next If PodeOcultar Then C.Visible = False End If Next Para carregar a datagridview uso um dataset, cujos dados vou buscar ao SQL, parte do codigo: Public Sub Carregar_datagrid_view() Dim DT As New DataTable DT = New DataTable("consultas") DT.Columns.Add("cod_relatorio") DT.Columns.Add("ano") DT.Columns.Add("mes") DT.Columns.Add("dia") Dim lista_d As New List(Of Colunas) lista_d = Colunas_BD.Filtro For Each coluna As Colunas In lista_d DT.Columns.Add(coluna.nome) 'Adiciono as colunas a partir de uma base de dados SQL Next Dim lista_produtos As New List(Of Ensaio) lista_produtos = Ensaios_BD.Qwery_Introduzido(" select top 100 ensaios.* from ensaios ") For Each registo As Ensaio In lista_produtos Dim novalinha As DataRow = DT.NewRow novalinha("cod_relatorio") = registo.cod_relatorio novalinha("ano") = registo.data.Year novalinha("mes") = registo.data.Month novalinha("dia") = registo.data.Day Dim ensaios_col As List(Of Ensaios_Colunas) = Ensaios_Colunas_BD.Lista_De_Ensaios_Colunas(registo.cod_relatorio) For Each coluna_ensaio As Ensaios_Colunas In ensaios_col Dim cooluna As Colunas = Colunas_BD.Procurar_Por_Codigo(coluna_ensaio.codigo)' Aqui procuro a coluna novalinha(cooluna.nome) = coluna_ensaio.valor 'Na respectiva coluna ponho lá valor, ou seja, há colunas que não estão ligadas com o ensaio, ficam nulas Next DT.Rows.Add(novalinha) Next dg.DataSource = DT End Sub Link to comment Share on other sites More sharing options...
Caça Posted February 23, 2012 at 03:42 PM Report Share #440859 Posted February 23, 2012 at 03:42 PM O que é que faz isto? If colunas_nao_preenchidas.Checked = True Then colunas_preenchidas.Checked = False Ocultar_Colunas_Vazias() ElseIf colunas_nao_preenchidas.Checked = False Then colunas_preenchidas.Checked = True colunas_nao_preenchidas.Checked = False End If Porque é que não usas RadioButtons? O código que te dei oculta as colunas que tem todas as linhas como DBNull.Value, acabei de testar. Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
obamaself Posted February 23, 2012 at 03:43 PM Author Report Share #440860 Posted February 23, 2012 at 03:43 PM não é quase a mesma coisa? O código até pode ocultar mas não percebo porque é que aqui não oculta. Link to comment Share on other sites More sharing options...
Caça Posted February 23, 2012 at 03:44 PM Report Share #440861 Posted February 23, 2012 at 03:44 PM O mais certo é estar ai a falhar alguma coisa, experimenta correr o código que te dei no clique de um botão a ver se funciona. Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
obamaself Posted February 23, 2012 at 04:07 PM Author Report Share #440866 Posted February 23, 2012 at 04:07 PM Já está. Eram alguns ensaios que tinham valores em branco -.- Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now