Insertion de deux espaces entre la date et l'heure dgv

adidass - Modifié le 28 mai 2022 à 16:40
dysorthographie Messages postés 92 Date d'inscription jeudi 27 janvier 2022 Statut Membre Dernière intervention 3 avril 2024 - 3 juin 2022 à 09:49
Bonjour,
merci de m'aider je veux insérer deux espaces dans une cellule date et heure
dans un tableau


Imports System.Data
Imports System.Data.OleDb
Imports System.IO


Public Class Form1
    Dim table As New DataTable("Table")
    Dim DtSet As DataSet
    ReadOnly MyCommand As OleDbDataAdapter
    ReadOnly com As New OleDbCommand


    Public table1 As New DataTable
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Importer.Click
        OpenFileDialog1.ShowDialog()

    End Sub

    Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        DataGridView1.Rows.Clear()

    
        Dim table As New DataTable("Table")
        Dim DtSet As DataSet
       
        Dim MyCommand As OleDbDataAdapter

        Dim chemin As String = OpenFileDialog1.FileName

        Dim MyConnection As New OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & chemin & ";Extended Properties=Excel 12.0;")

        MyCommand = New OleDbDataAdapter("select * from [Feuil1$]", MyConnection)

        DtSet = New DataSet

        MyCommand.Fill(DtSet)

        DataGridView1.DataSource = DtSet.Tables(0)

         MyConnection.Close()



    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles traitemnt.Click

        Dim dgvcolumnsNames As List(Of String) = DataGridView1.Columns.
            Cast(Of DataGridViewColumn).ToList().Select(Function(c) c.Name).ToList()

        Dim lengeurno As Int32
        Dim lengeurdate As Int32
        Dim no As String = Nothing
        Dim EnNo As String = Nothing
        Dim teste1 As String = Nothing

        Dim teste As String
        For x As Integer = 0 To DataGridView1.Rows.Count - 1

            'à chaque ligne lu on insert les valeurs dans DataGridView2
           DataGridView1.Rows(x).Cells(1).Value, "", "1", DataGridView1.Rows(x).Cells(1).Value)
         DataGridView1.Rows(x).Cells(1).Value)
              
            Dim s As String = DataGridView1.Rows(x).Cells(2).Value
            Dim subs As String() = s.Split(" "c, Char.Parse(" "))

            For Each substring In subs
                teste = s
            Next
            MsgBox(subs)
          

            If DataGridView1.Rows(x).Cells(0).Value.ToString <> "" Then
                    If DataGridView1.Rows(x).Cells(0).Value.ToString.Length = 5 Then

                        no = DataGridView1.Rows(x).Cells(0).Value
                    Else

                        lengeurno = 5 - DataGridView1.Rows(x).Cells(0).Value.ToString.Length

                    For t As Integer = 0 To lengeurno - 1
                        no &= "0"
                    Next
                    no &= DataGridView1.Rows(x).Cells(0).Value
                    End If
               

                    If DataGridView1.Rows(x).Cells(1).Value.ToString <> "" Then
                        If DataGridView1.Rows(x).Cells(1).Value.ToString.Length = 8 Then

                            EnNo = DataGridView1.Rows(x).Cells(1).Value
                        Else

                            lengeurno = 8 - DataGridView1.Rows(x).Cells(1).Value.ToString.Length

                        For t As Integer = 0 To lengeurno - 1
                            EnNo &= "0"
                        Next
                        EnNo &= DataGridView1.Rows(x).Cells(1).Value
                        End If
                      
                        DataGridView2.Rows.Add(no, "1", EnNo, "", "1", "0", dataGridView1.Rows(x).Cells(2).Value)
                    
                        no = Nothing
                        EnNo = Nothing
                    teste1 = Nothing
           
                End If
                End If
               
            Next
       
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles export.Click
        Dim writer As TextWriter = New StreamWriter("C:\folder\Text.txt")
    
        Dim dgvcolumnsNames As List(Of String) = DataGridView1.Columns.
            Cast(Of DataGridViewColumn).ToList().Select(Function(c) c.Name).ToList()

        For i As Integer = 0 To DataGridView2.Rows.Count - 2 Step +1

            For j As Integer = 0 To DataGridView2.Columns.Count - 1 Step +1

                writer.Write(DataGridView2.Rows(i).Cells(j).Value.ToString() & vbTab & "")

            Next

            writer.WriteLine("")
        Next
        writer.Close()

        MessageBox.Show("Data Exported")

    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs)

    End Sub
End Class



EDIT : Ajout des balises de code (la coloration syntaxique).
Explications disponibles ici : ICI

Merci d'y penser dans tes prochains messages.

2 réponses

je trouver la solution


Dim teste As String

teste = Mid(DataGridView1.Rows(x).Cells(2).Value, 1, 11 - 1) & " " & Mid(DataGridView1.Rows(x).Cells(2).Value, 11, Len(DataGridView1.Rows(x).Cells(2).Value))

 DataGridView2.Rows.Add(no, "1", EnNo, "          ", "1", "0", teste)
0
dysorthographie Messages postés 92 Date d'inscription jeudi 27 janvier 2022 Statut Membre Dernière intervention 3 avril 2024 5
Modifié le 3 juin 2022 à 09:51
Bonjour,
Vue que dans ton connectionstring tu ne défini pas la première ligne. Comme étant les entêtes de colonnes le nom des champs sont:
F1 pour la colonne A, F2 pour la colonne B ,F3 etc.

Donc tu peux formater tes dates dans ta requête. Supposons que la date ce trouve en C!
OleDbDataAdapter("select format([F3],'dd/mm/yyyy  hh:mm:ss')from [Feuil1$]")
0
Rejoignez-nous