none
Qual a melhor forma de salvar um DataGridView com seus dados contidos nele? RRS feed

  • Pergunta

  • Olá. Alguém pode me dizer qual a forma mais correta de salvar um DataGridView e depois carrega-lo?

    Criei essa rotina para salvar e depois carregar. Alguém por favor pode me dizer porque dá esse erro?

    É só copiar e colar e ver o erro que dá. Alguém me dê essa força por favor.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

       
            DataGridView1.Columns.Clear()

            DataGridView1.Visible = True

            DataGridView1.Size = New Size(870, 800)

            DataGridView1.Location = New Point(800, 40)

            DataGridView1.[ReadOnly] = True

            DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize 'determina o tamanho do cabeçalho de forma automática

            'adiciona as colunas
            '**************************************************************************************
            For x = 0 To 20
                DataGridView1.Columns.Add("Col " & x, "Coluna " & x) 'adicionar as colunas
            Next
            '**************************************************************************************
            For v = 0 To 20 'centraliza as dezenas nas colunas
                DataGridView1.Columns(v).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter 'culunas dos dados
                DataGridView1.Columns(v).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter 'colunas do cabeçalho
            Next
            '**************************************************************************************
            'define a largura das colunas
            DataGridView1.Columns(0).Width = 60 'coluna do número da aposta
            DataGridView1.Columns(1).Width = 67 'coluna da data do teste
            DataGridView1.Columns(2).Width = 60 'coluna do número do teste
            '**************************************************************************************
            For v = 3 To 17
                DataGridView1.Columns(v).Width = 27 'colunas das 20 dezenas
                DataGridView1.Columns(v).DefaultCellStyle.ForeColor = Color.Navy
            Next
            '**************************************************************************************
            DataGridView1.Columns(18).Width = 40 'coluna dos acertos
            '**************************************************************************************
            DataGridView1.Columns(19).Width = 85 'coluna do custo por aposta
            '**************************************************************************************
            DataGridView1.Columns(20).Width = 105 'coluna do Valor Líquido do Prêmio
            '**********************************************************************************
            DataGridView1.Columns(0).HeaderText = "Número da Aposta"
            DataGridView1.Columns(1).HeaderText = "Data do Teste"
            DataGridView1.Columns(2).HeaderText = "Número do Teste"
            DataGridView1.Columns(3).HeaderText = "01"
            DataGridView1.Columns(4).HeaderText = "02"
            DataGridView1.Columns(5).HeaderText = "03"
            DataGridView1.Columns(6).HeaderText = "04"
            DataGridView1.Columns(7).HeaderText = "05"
            DataGridView1.Columns(8).HeaderText = "06"
            DataGridView1.Columns(9).HeaderText = "07"
            DataGridView1.Columns(10).HeaderText = "08"
            DataGridView1.Columns(11).HeaderText = "09"
            DataGridView1.Columns(12).HeaderText = "10"
            DataGridView1.Columns(13).HeaderText = "11"
            DataGridView1.Columns(14).HeaderText = "12"
            DataGridView1.Columns(15).HeaderText = "13"
            DataGridView1.Columns(16).HeaderText = "14"
            DataGridView1.Columns(17).HeaderText = "15"
            DataGridView1.Columns(18).HeaderText = "Ac."
            DataGridView1.Columns(19).HeaderText = "Custo/Aposta R$"
            DataGridView1.Columns(20).HeaderText = "Valor Líquido Prêmio R$"
            '**************************************************************************************
            DataGridView1.RowsDefaultCellStyle.BackColor = Color.Lavender
            DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige

            'define a cor do cabeçalho
            Dim style1 As DataGridViewCellStyle = DataGridView1.ColumnHeadersDefaultCellStyle
            style1.BackColor = Color.Aqua
            style1.ForeColor = Color.Black
            style1.Font = New Font(DataGridView1.Font, FontStyle.Bold)

            DataGridView1.DefaultCellStyle.SelectionForeColor = Color.White 'torna branca a cor da fonte quando selecionada
            DataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black 'torna preta a cor da célula quando selecionada
            DataGridView1.DefaultCellStyle.Font = New Font("Arial Black", 8) 'usa a fonte Arial Black no tamanho 8
            DataGridView1.RowHeadersVisible = False 'retira a primeira coluna da grade

            DataGridView1.EnableHeadersVisualStyles = False

            DataGridView1.DefaultCellStyle.ForeColor = Color.Blue 'torna azul a cor dos dados e geral

            For u = 1 To 2
                DataGridView1.Rows.Add(u.ToString().PadLeft(5, "0"c), "13/04/21", "2.204", "01", "03", "05", "07", "09", "10", "12", "13", "15", "16", "17", "20", "21", "23", "25", "12", "2,50", "10,00")
            Next

        End Sub

        'salva no arquivo "C:\Usuario\Home\TesteSalvarDataGrid.txt"
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

            Dim W As IO.StreamWriter

            W = New IO.StreamWriter("C:\Usuario\Home\TesteSalvarDataGrid.txt")


            For lin = 0 To 1

                For col = 0 To 20

                    W.WriteLine(DataGridView1.Rows(lin).Cells(col).Value)

                Next

            Next

            W.Close()

        End Sub

        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

            'configura o DataGirdView1
            '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

            DataGridView1.Columns.Clear()

            DataGridView1.Visible = True

            DataGridView1.Size = New Size(870, 800)

            DataGridView1.Location = New Point(800, 40)

            DataGridView1.[ReadOnly] = True

            DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize 'determina o tamanho do cabeçalho de forma automática

            'adiciona as colunas
            '**************************************************************************************
            For x = 0 To 20
                DataGridView1.Columns.Add("Col " & x, "Coluna " & x) 'adicionar as colunas
            Next
            '**************************************************************************************
            For v = 0 To 20 'centraliza as dezenas nas colunas
                DataGridView1.Columns(v).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter 'culunas dos dados
                DataGridView1.Columns(v).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter 'colunas do cabeçalho
            Next
            '**************************************************************************************
            'define a largura das colunas
            DataGridView1.Columns(0).Width = 60 'coluna do número da aposta
            DataGridView1.Columns(1).Width = 67 'coluna da data do teste
            DataGridView1.Columns(2).Width = 60 'coluna do número do teste
            '**************************************************************************************
            For v = 3 To 17
                DataGridView1.Columns(v).Width = 27 'colunas das 15 dezenas
                DataGridView1.Columns(v).DefaultCellStyle.ForeColor = Color.Navy
            Next
            '**************************************************************************************
            DataGridView1.Columns(18).Width = 40 'coluna dos acertos
            '**************************************************************************************
            DataGridView1.Columns(19).Width = 85 'coluna do custo por aposta
            '**************************************************************************************
            DataGridView1.Columns(20).Width = 105 'coluna do Valor Líquido do Prêmio
            '**********************************************************************************
            DataGridView1.Columns(0).HeaderText = "Número da Aposta"
            DataGridView1.Columns(1).HeaderText = "Data do Teste"
            DataGridView1.Columns(2).HeaderText = "Número do Teste"
            DataGridView1.Columns(3).HeaderText = "01"
            DataGridView1.Columns(4).HeaderText = "02"
            DataGridView1.Columns(5).HeaderText = "03"
            DataGridView1.Columns(6).HeaderText = "04"
            DataGridView1.Columns(7).HeaderText = "05"
            DataGridView1.Columns(8).HeaderText = "06"
            DataGridView1.Columns(9).HeaderText = "07"
            DataGridView1.Columns(10).HeaderText = "08"
            DataGridView1.Columns(11).HeaderText = "09"
            DataGridView1.Columns(12).HeaderText = "10"
            DataGridView1.Columns(13).HeaderText = "11"
            DataGridView1.Columns(14).HeaderText = "12"
            DataGridView1.Columns(15).HeaderText = "13"
            DataGridView1.Columns(16).HeaderText = "14"
            DataGridView1.Columns(17).HeaderText = "15"
            DataGridView1.Columns(18).HeaderText = "Ac."
            DataGridView1.Columns(19).HeaderText = "Custo/Aposta R$"
            DataGridView1.Columns(20).HeaderText = "Valor Líquido Prêmio R$"
            '**************************************************************************************
            DataGridView1.RowsDefaultCellStyle.BackColor = Color.Lavender
            DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige

            'define a cor do cabeçalho
            Dim style1 As DataGridViewCellStyle = DataGridView1.ColumnHeadersDefaultCellStyle
            style1.BackColor = Color.Aqua
            style1.ForeColor = Color.Black
            style1.Font = New Font(DataGridView1.Font, FontStyle.Bold)

            DataGridView1.DefaultCellStyle.SelectionForeColor = Color.White 'torna branca a cor da fonte quando selecionada
            DataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black 'torna preta a cor da célula quando selecionada
            DataGridView1.DefaultCellStyle.Font = New Font("Arial Black", 8) 'usa a fonte Arial Black no tamanho 8
            DataGridView1.RowHeadersVisible = False 'retira a primeira coluna da grade

            DataGridView1.EnableHeadersVisualStyles = False

            DataGridView1.DefaultCellStyle.ForeColor = Color.Blue 'torna azul a cor dos dados e geral

            Dim R As IO.StreamReader

            R = New IO.StreamReader("C:\Usuario\Home\TesteSalvarDataGrid.txt")

            For lin = 0 To 1

                For col = 0 To 20

                    DataGridView1.Rows(0).Cells(col).Value = R.ReadLine

                Next

            Next

            R.Close()

        End Sub

    End Class

    quinta-feira, 15 de abril de 2021 01:33

Todas as Respostas

  • Ola Durval,

    Qual e o erro? Não está vendo

    Você quer salvar para um banco de dados ou arquivo?

    O vídeo abaixo pode ajudar com a sua dúvida

    https://www.youtube.com/watch?v=CGyn4Deo6fA


    segunda-feira, 19 de abril de 2021 15:41
    Moderador
  • Valeu, obrigado Maurício. Achei porque dava erro, eu esqueci de adicionar linhas na grade.

    quarta-feira, 21 de abril de 2021 17:14