Jump to content

Alinha coluna do datagriview


igadino
 Share

Recommended Posts

Boa Noite alguém pode me um help para alinha exporta só que não estou conseguindo 
fazer o alinhamento das colunas com valores para direita

 

Public Sub Exportar()
        Dim sfg As New SaveFileDialog
        sfg.FileName = ''
        sfg.Filter = 'Txt|*.txt'
        sfg.Title = ' Salvar/Exportar'
        If sfg.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim columnSizes As List(Of Integer) = New List(Of Integer)()
            For i As Integer = 0 To FrmCadastroProdutos.dgvProdutos.Columns.Count - 1
                columnSizes.Add(FrmCadastroProdutos.dgvProdutos.Columns(i).HeaderText.Length)
            Next
            For Each row As DataGridViewRow In FrmCadastroProdutos.dgvProdutos.Rows
                For i As Integer = 0 To FrmCadastroProdutos.dgvProdutos.Columns.Count - 1
                    Dim valueString As String = Convert.ToString(row.Cells(i).Value)
                    If valueString.Length > columnSizes(i) Then columnSizes(i) = valueString.Length
                Next
            Next
            Using writer As StreamWriter = New StreamWriter(sfg.FileName, False)
                For i As Integer = 0 To FrmCadastroProdutos.dgvProdutos.Columns.Count - 1
                    Dim headerText As String = FrmCadastroProdutos.dgvProdutos.Columns(i).HeaderText
                    writer.Write(headerText)
                    If headerText.Length < columnSizes(i) Then writer.Write(New String(' 'c, columnSizes(i) - headerText.Length))
                    writer.Write('|')
                Next
                writer.WriteLine('')
                writer.WriteLine('===========================================================================================================================')
                For Each row As DataGridViewRow In FrmCadastroProdutos.dgvProdutos.Rows
                    For i As Integer = 0 To FrmCadastroProdutos.dgvProdutos.Columns.Count - 1
                        Dim valueString As String = Convert.ToString(row.Cells(i).Value)
                        writer.Write(valueString)
                        If valueString.Length < columnSizes(i) Then writer.Write(New String(' 'c, columnSizes(i) - valueString.Length))
                        writer.Write('|')
                    Next
                    writer.WriteLine('')
                    writer.WriteLine('---------------------------------------------------------------------------------------------------------------------------')
                Next
            End Using
        End If
    End Sub 

 

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.