Results 1 to 3 of 3

Thread: [RESOLVED] Open File Dialogue -- Datagridview

  1. #1

    Thread Starter
    Lively Member GTDriver's Avatar
    Join Date
    Apr 2015
    Location
    Blighty
    Posts
    66

    Resolved [RESOLVED] Open File Dialogue -- Datagridview

    Hi all

    The following code uses a OFD to import an Excel spreadsheet to a a DGV.

    Code:
       Private Sub btnImport_Click(sender As Object, e As EventArgs) Handles btnImport.Click
    
            Dim strConnectionString$ = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}; Extended Properties=""Excel 12.0; HDR=No;IMEX=1"""
    
    
    
            'DataGridView1.DataSource.rows.clear()
            If ofdSelectFile.ShowDialog = Windows.Forms.DialogResult.OK Then
                strFileName = ofdSelectFile.FileName
                Dim strExcelFileName$ = String.Format(strConnectionString, strFileName)
    
                con = New OleDbConnection(strExcelFileName)
                DA = New OleDbDataAdapter("select * from [Sheet1$]", con)
                DT.Reset()
                DA.Fill(DT)
    
                DataGridView1.DataSource = DT
    
                con.Close()
            Else
            End If
    
        End Sub
    The code works fine... except if i load the OFD and select a new file. If i do this, the DGV doesnt load anything... i mean, its litrally empty, even if i try loading the orignal file.

    I've tried adding code to refresh the DGV, i've tried nulling the datasource before loading the new datatable. I'm clean out of ideas. Any help appreciated.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Open File Dialogue -- Datagridview

    Don't reset the DT... clear it. Or create a new one.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Lively Member GTDriver's Avatar
    Join Date
    Apr 2015
    Location
    Blighty
    Posts
    66

    Re: Open File Dialogue -- Datagridview

    Argh - why didnt i think of that! Cheers techgnome .

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width