SETHA SITE INFORMATION

  • Increase font size
  • Default font size
  • Decrease font size
SETHA SITE INFORMATION

Export Data from DataGridView in Visual Basic 2008 to Microsoft Word Table

E-mail Print PDF

This article shows you how to write code in Visual Basic 2008 to export data from DataGridView control into Microsoft Word's Table.


Dim objWordApp As Object
Dim objWordDoc As Object

objWordApp = CreateObject("Word.Application")
objWordDoc = objWordApp.documents.add()

objWordDoc.tables.add(objWordDoc.range(0, 0), 11, 3)
With objWordDoc.tables(1)
     .borders.InsideLineStyle = 1
     .borders.OutsideLineStyle = 1

     For i As Integer = 0 To 10
          .rows(i + 1).cells(1).range.text = Me.dgvData.Rows(i).Cells("id").Value
          .rows(i + 1).cells(2).range.text = Me.dgvData.Rows(i).Cells("name").Value
          .rows(i + 1).cells(3).range.text = Me.dgvData.Rows(i).Cells("gender").Value
     Next
End With

objWordApp.visible = True

objWordApp = Nothing

Download Sample Code Here
 

Using DataGridView in VB 2008 to View Data from MS-Access Table

E-mail Print PDF

This article show you how to write code in Visual Basic 2008 to view data from Table in Microsoft Access 2003 using DataGridView control. Assume that the database file named data.mdb is placed into ...\bin\debug\ folder (...\bin\debug\data.mdb"), so that you can use Application.StartUpPath & "\data.mdb" in VB Code.

Dim cn As New OleDb.OleDbConnection
Dim da As OleDb.OleDbDataAdapter
Dim dt As New DataTable

cn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" & _
                      Application.StartupPath & "\data.mdb"
cn.Open()

da = New OleDb.OleDbDataAdapter("SELECT [name], gender, phone, address " & _
                                "FROM student ORDER BY [name]", cn)
da.Fill(dt)

Me.DataGridView1.DataSource = dt

cn.Close()

Download Sample Code Here

Another way, we can use [for ... next] loop to get data from DataTable row by row into DataGridView row.

        'add column to datagridview
        With Me.DataGridView1
            .Columns.Add("name", "Name")
            .Columns.Add("gender", "Gender")
            .Columns.Add("phone", "Phone")
            .Columns.Add("address", "Address")

            .AllowUserToAddRows = False
            .EditMode = DataGridViewEditMode.EditProgrammatically
        End With

        'get data
        Dim cn As New OleDb.OleDbConnection
        Dim da As OleDb.OleDbDataAdapter
        Dim dt As New DataTable

        cn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" & _
                                Application.StartupPath & "\data.mdb"
        cn.Open()

        da = New OleDb.OleDbDataAdapter("SELECT [name], gender, phone, address " & _
                                      "FROM student ORDER BY [name]", cn)
        da.Fill(dt)

        For Each dr As DataRow In dt.Rows
            Me.DataGridView1.Rows.Add()
            With Me.DataGridView1.Rows(Me.DataGridView1.Rows.Count - 1)
                .Cells("name").Value = dr("name")
                .Cells("gender").Value = dr("gender")
                .Cells("phone").Value = dr("phone")
                .Cells("address").Value = dr("address")
            End With
        Next

        cn.Close()
Download Sample Code Here
 

Create Crystal Report from Visual Basic .Net 2008 using Data in DataGridView

E-mail Print PDF
This article will show you how to create crystal report using ADO.Net in Visual Basic .Net 2008.
  • Step 1: Prepare data in DataGridView to be placed on CrystalReport

    With Me.DataGridView1
         .Columns.Add("code", "Code")
         .Columns.Add("name", "Name")
         .Columns.Add("gender", "Gender")
         .Columns.Add("address", "Address")
    
         .EditMode = DataGridViewEditMode.EditProgrammatically
         .AllowUserToAddRows = False
    End With
    
    'add data to datagridview
    'example data in 10 rows
    For i As Integer = 1 To 0
        Me.DataGridView1.Rows.Add()
        With Me.DataGridView1.Rows(i)
             .Cells("code").Value = Format(i + 1, "0000")
             .Cells("name").Value = "Student Name" & i + 1
             .Cells("gender").Value = "M"
             .Cells("address").Value = "PP"
         End With
    Next
    
  • Step 2: Create DataSet and DataTable for Crystal Report
    • Click on Project Menu
    • Click on Add New Item
    • Choose DataSet (Enter DataSet name)
    • Create DataTable in DataSet (Same columns in step 1)
  • Step 3: Create and Design Crystal Report using Data from DataTable in Step 2
    • Click on Project Menu
    • Click on Add New Item
    • Choose Reporting -> Crystal Report
    • Choose Create As Blank Report
    • Right Click on Database Field in Field Explorer of Crystal Report
    • Choose Database Expert
    • Choose Project Data -> ADO.Net DataSets -> (DataSet and DataTable Created in Step 2)
    • OK
    • Drag Each Field in Database Fields into Crystal Report
  • Step 4: Create Form to View Crystal Report using CrystalReportViewer control
    • Create New Form
    • Draw CrystalReportViewer Control on Form
    • Coding
    Dim dt As New DataTable
    '
    With dt
         .Columns.Add("code")
         .Columns.Add("name")
         .Columns.Add("gender")
         .Columns.Add("address")
    End With
    '
    For Each dr As DataGridViewRow In Me.DataGridView1.Rows
         dt.Rows.Add(dr.Cells("code").Value, dr.Cells("name").Value, _
                     dr.Cells("gender").Value, dr.Cells("address").Value)
    Next
    '
    Dim rptDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
    rptDoc = New CrystalReport1
    rptDoc.SetDataSource(dt)
    '
    Form2.CrystalReportViewer1.ReportSource = rptDoc
    Form2.ShowDialog()
    Form2.Dispose()
    

Download Sample Here
 
See Video Below
 

Create Brick Game in Turbo C++

E-mail Print PDF
This article shows you the creation of BRICK GAME in Turbo C++. From this source code you will know how to:
  • Use Graphic Mode in Turbo C++
  • Use Mouse Pointer in Turbo C++
  • Use OOP in Turbo C++
  • Create Brick Game in Turbo C++

Please download source code below!

Download Sample Code Here
 

How to Connect from VB 2008 to MS-Access 2003 with Password Protection

E-mail Print PDF
This article shows you how to program from Visual Basic .Net 2008 connecting with Microsoft Access Database 2003 with database password protection. How to set database password in Microsoft Access 2007?
Dim cn As New OleDb.OleDbConnection
Dim da As OleDb.OleDbDataAdapter
Dim dt As New DataTable
'Set connection string to database
cn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" & _
                      Application.StartupPath & "\data.mdb; " & _
                      "Jet OLEDB:Database Password=12345"
'open connection
cn.Open()
'connect to table to get data
da = New OleDb.OleDbDataAdapter("SELECT [name], gender, phone, address " & _
                                "FROM student ORDER BY [name]", cn)
'fill data from access table into datatable
da.Fill(dt)
'fill data from datatable into datagridview
Me.DataGridView1.DataSource = dt
'close connection
cn.Close()
Download Sample Code Here
 


Page 12 of 19