C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » DataGridView »

Populate DataGridView From Excel Sheet


Posted Date: 12 Jun 2009    Resource Type: Code Snippets    Category: DataGridView
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 10



Populate DataGridView From Excel Where First Name of Employee starts with 'C'
Need to include the reference Microsoft DAO 3.6


Imports System.Data
Imports System.Data.OleDb

Private Sub btnPopulateGrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPopulateGrid.Click

Const stFile As String = "E:\EmployeeExcels\Employee.xls"

Const stCon As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & stFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=YES"";"

Dim dao_dbE As dao.DBEngine
Dim dao_DB As dao.Database
Dim strFirstSheetName As String

Try
'To get the Excel Sheet's Name
dao_dbE = New dao.DBEngine
dao_DB = dao_dbE.OpenDatabase("E:\EmployeeExcels\Employee.xls", False, True, "Excel 8.0;")

strFirstSheetName = dao_DB.TableDefs(1).Name

'The range is named Source and it also contains the fieldnames.

'The fieldnames are automatically added to the Datagrid columnnames.

Dim stSQL As String = "SELECT * FROM [" & strFirstSheetName & "] WHERE [First Name] LIKE 'C%'"

Dim cn As New OleDbConnection(stCon)

cn.Open()

Dim da As New OleDbDataAdapter(stSQL, cn)

'The Datasets name is viewed on top of the Datagrid.

Dim dt As New DataTable
'Populate the Datatable

da.Fill(dt)


'Populate the DataGridView
With Me.dbGridView
.DataSource = dt
.Refresh()

End With

da.Dispose()


' Adjust the column widths based on the displayed values.
Me.dbGridView.AutoResizeColumns( _
DataGridViewAutoSizeColumnsMode.DisplayedCells)

Catch ex As Exception
MsgBox(ex.ToString)

Finally
'Release objects from memory.

If Not dt Is Nothing Then
dt = Nothing
End If
If Not cn is Nothing Then
If cn.State = ConnectionState.Open Then
cn.Close()
End If
cn = Nothing
End If

End Try

End Sub




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Populate DataGridView From Excel Sheet  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: selected row content of a datagridview
Previous Resource: Multi view sample code
Return to Discussion Resource Index
Post New Resource
Category: DataGridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use