How to display Data on a Data Grid View

(VB 2005+2008 Programming : SQL 2005)

Hi developers,

I know that you all know how to use typed dataset. But when coding...
Things are different now than SQL 2000 to excute a command. So this is the code that will help you to begin with SQL 2005 Express and Adapters.

This example will show a data grid view and fill it with data from a database

This is the form design :

_
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
_
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
_
Private Sub InitializeComponent()
Me.DataGridView = New System.Windows.Forms.DataGridView
CType(Me.DataGridView, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGridView
'
Me.DataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.DataGridView.Location = New System.Drawing.Point(12, 12)
Me.DataGridView.Name = "DataGridView"
Me.DataGridView.RowTemplate.Height = 24
Me.DataGridView.Size = New System.Drawing.Size(355, 243)
Me.DataGridView.TabIndex = 0
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(379, 267)
Me.Controls.Add(Me.DataGridView)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGridView, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Friend WithEvents DataGridView As System.Windows.Forms.DataGridView
End Class

Now This how to fill the DGV don't forget to change the connection string with your connection string !


Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Store a connection string
Dim connectionstring As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Omar Abid\Mes Documents\data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
'Create new SQL Connection
Dim sqlconnection As New SqlConnection(connectionstring)
Using sqlconnection
Dim sqlcommand As SqlCommand = sqlconnection.CreateCommand
Using sqlcommand
sqlcommand.CommandType = CommandType.Text
sqlcommand.CommandText = "select * from table1"
Dim adapter As New SqlDataAdapter(sqlcommand)
Using adapter
Dim datatable As New DataTable("user")
Using datatable
adapter.Fill(datatable)
DataGridView.DataSource = datatable
End Using
End Using
End Using
End Using
End Sub
End Class

Hope it helps some begineer in some ways !

Have nice time on programming ;)

0 comments:

Post a Comment