Bonjour,

J'essaie d'exporter les données d'un dataGrid via un fichier Excel en m'inspirant d'un post se trouvant dans la FAQ.

J'ai l'erreur suivante :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
 
Les données ne peuvent être lues à partir d'un DataGrid qui n'es pas lié à un DataTable
Pourtant, je lie bien mon DataGrid à une DataTable.

Voici mon code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 
     Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheet As Excel.Worksheet
 
        Dim i As Integer
        Dim nbRow As Integer = 0
 
        Dim dgDemAtten As New DataGrid
        Dim dsDemAtten As New DataSet
        Dim dtDemAtten As New DataTable
        Dim oAdapterDemAtten As New System.data.oledb.OleDbDataAdapter
        Dim oConDemAtten As New System.data.oledb.OleDbConnection
        Dim oBuildDemAtten As New System.data.oledb.OleDbCommandBuilder
 
        ' Réinitialiser la liste des prestations en litige
        Dim FIni As New FichierIni
        Dim cRubrique As String = "BDD" 'Nom de la rubrique
        Dim cKey As String = "Prestations" 'Nom de la clé
        Dim cRepertoire As String 'Variable récupérant la string 
        Dim istat As Integer
 
        Dim cIniFile
        cIniFile = FIni.AppPath(True) & "Fichier.INI"
 
        'Appel de la fonction
        istat = FIni.Get_Private_Profile_String(cRubrique, cKey, "", cRepertoire, cIniFile)
        Dim strConn As String = "Provider=SQLOLEDB;" & cRepertoire
 
        xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
        xlBook = CType(xlApp.Workbooks.Add, Excel.Workbook)
        xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)
 
        ' ici on compte le nombre de lignes et de colonnes du datatable
        Dim nbrLigne As Integer = DS_DDF1.Tables("Table").Rows.Count - 1
        Dim nbrColon As Integer = DS_DDF1.Tables("Table").Columns.Count - 1
        Dim x, y As Integer
 
        'Création requête SQL
strSlctDemFondAss = SELECT ...................
 
        If nbRow < 1 Then
            'Si pas de saisi, on annule l'opération
            MsgBox("Aucune demande n'a été sélectionné", MsgBoxStyle.Information, "Information")
            MsgBox("Opération annulée", MsgBoxStyle.Information, "Information")
            Exit Sub
        End If
 
        Try
            'Donner à la propriété ConnectionString les paramètres de connexion
            oConDemAtten.ConnectionString = strConn
            'Ouverture de la connexion
            oConDemAtten.Open()
 
            'Instancier un objet Adapter
            oAdapterDemAtten = New System.data.oledb.OleDbDataAdapter(strSlctDemFondAss, strConn)
 
            'initialiser l'objet OleCBComandBuilder (sinon pas d'update)
            oBuildDemAtten = New System.data.oledb.OleDbCommandBuilder(oAdapterDemAtten)
 
            'Avec l'aide de la propriété Fill du DataAdapter charger le DataSet
            oAdapterDemAtten.Fill(dsDemAtten, "Table")
 
            dtDemAtten = dsDemAtten.Tables("Table")
 
            'Mettre dans le DataGrid une table DataTable
            dgDemAtten.DataSource = dtDemAtten
 
            For x = 0 To nbrColon
                ' ici on prends le titre des colonnes du datatable
                xlSheet.Cells(1, x + 1) = dsDemAtten.Tables("Table").Columns(x).ColumnName
                ' on mets la première ligne en gras
                xlSheet.Rows(1).Font.Bold = True
 
                ' pour chaque colonne et chaque ligne on transfert les données      
                For y = 0 To nbrLigne
                    xlSheet.Cells(y + 2, x + 1) = dgDemAtten.Item(y, x)
                Next
            Next
 
            ' ici on affiche les résultat dans excel
            xlSheet.Application.Visible = True
            ' on peut sauvegarder notre document sur le disque
            xlSheet.SaveAs("C:\fichier.xls")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
 
        ' on quitte l'application et on détruit les objets
        xlApp.Quit()
        xlSheet = Nothing
        xlBook = Nothing
        xlApp = Nothing
Pouvez-vous m'aider svp?