Click here to Skip to main content
15,917,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DataGridView that I populate from an Excel file. The problem is I want to add the Excel file to my resources (if possible) then populate the DataGridView from there.

I use this now:
VB
Try
    Dim MyConnection As System.Data.OleDb.OleDbConnection
    'Dim DtSet As System.Data.DataSet
    Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
    MyConnection = New System.Data.OleDb.OleDbConnection _
            ("provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\coded\Vehicle\Vehicles.xls';Extended Properties=Excel 8.0;")
    MyCommand = New System.Data.OleDb.OleDbDataAdapter _
                ("select * from [Sheet1$]", MyConnection)
    MyCommand.TableMappings.Add("Table", "TestTable")
    'DtSet = New System.Data.DataSet
    MyCommand.Fill(DataSet1)
    DataGridView1.DataSource = DataSet1.Tables(0)
    MyConnection.Close()
Catch ex As Exception
    MsgBox(ex.ToString)
End Try
Posted
Updated 18-Feb-12 4:56am
v2
Comments
André Kraak 18-Feb-12 10:57am    
Edited question:
Added pre tags

1 solution

Do not put the Excel file in your resources. You're just going to have to write it to a folder and to get at the data anyway, so what's the point?

If you want the data in your Resources, you'll have to move the data to another format, such as an XML file, in to put the data in Resources and avoid writing it back to a file.

Why? The .NET XML classes are capable of reading data from a Stream object, whereas Excel and the data providers that work with it cannot. Those providers do not know anything about a .NET Stream object, limiting them to using actual files on disk.
 
Share this answer
 
Comments
wiswalld 18-Feb-12 11:46am    
I added an xml file to my project but when it tries to open it say it cannot find the file

Dim dsPubs As New DataSet()
dsPubs.ReadXml("Vehicles.xml")
Me.DataGridView1.DataSource = dsPubs.Tables(0)


Me.ComboBox1.DataSource = dsPubs.Tables(0)
Me.ComboBox1.DisplayMember = "MAKE_MODEL_DESCRIPTION"

Me.ComboBox2.DataSource = dsPubs.Tables(0)
Me.ComboBox2.DisplayMember = "MODEL_DESCRIPTION"
wiswalld 18-Feb-12 11:51am    
Forget it. It was not in my bin folder.
Dave Kreskowiak 18-Feb-12 12:36pm    
<smacks forehead> So you just forgot about putting the file in your Resources, huh?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900