Jump to content

Datagridview Prompt


obamaself
 Share

Recommended Posts

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

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

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

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

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

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.