[VB] [SQL Server] ตัวแปร myRepeater จาก code vb.net ตัวนี้คือ DataGridViewใช่รึเปล่าครับ
ครับ DataGridView ก็สามารถใช้ DataSource ของ Data Reader ได้ครับ
Date :
2012-01-17 09:42:15
By :
webmaster
ลองดูแบบนี้ครับ
Code (VB.NET)
'*** BindData From Database **'
Dim objConn As OleDbConnection
Dim dtAdapter As OleDbDataAdapter
Dim dt As New DataTable
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydatabase.mdb"
objConn = New OleDbConnection(strConnString)
objConn.Open()
Dim strSQL As String
strSQL = "SELECT * FROM customer"
dtAdapter = New OleDbDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Me.myDgv.DataSource = dt
'******************************'
Me.myDgv.Columns.Clear()
With myDgv.ColumnHeadersDefaultCellStyle
.BackColor = Color.Navy
.ForeColor = Color.White
.Font = New Font(songsDataGridView.Font, FontStyle.Bold)
End With
Dim ColumnTextBox As DataGridViewTextBoxColumn
'*** Column (Select) ***'
Dim ColumnCheckBox As New DataGridViewCheckBoxColumn()
ColumnCheckBox.Width = 30
ColumnCheckBox.DataPropertyName = "Select"
Me.myDgv.Columns.Add(ColumnCheckBox)
'*** Checkbox in Header ***'
Dim ckBox As New CheckBox()
Dim rect As Rectangle = myDgv.GetCellDisplayRectangle(0, -1, True)
ckBox.Size = New Size(18, 18)
rect.X = rect.Location.X + (rect.Width / 4)
ckBox.Location = rect.Location
AddHandler ckBox.CheckedChanged, New EventHandler(AddressOf ckBox_CheckedChanged)
Me.myDgv.Controls.Add(ckBox)
myDgv.Columns(0).Frozen = False
'*** Column (CustomerID) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "CustomerID"
ColumnTextBox.HeaderText = "CustomerID"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.myDgv.Columns.Add(ColumnTextBox)
'*** Column (Name) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Name"
ColumnTextBox.HeaderText = "Name"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.myDgv.Columns.Add(ColumnTextBox)
'*** Column (Email) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Email"
ColumnTextBox.HeaderText = "Email"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.myDgv.Columns.Add(ColumnTextBox)
'*** Column (CountryCode) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "CountryCode"
ColumnTextBox.HeaderText = "CountryCode"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.myDgv.Columns.Add(ColumnTextBox)
'*** Column (Budget) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Budget"
ColumnTextBox.HeaderText = "Budget"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.myDgv.Columns.Add(ColumnTextBox)
'*** Column (Used) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Used"
ColumnTextBox.HeaderText = "Used"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.myDgv.Columns.Add(ColumnTextBox)
Date :
2012-01-17 09:45:38
By :
webmaster
ตอนนี้ยังรันไม่ได้ครับ ขอถามหน่อยนะครับ
ตอนแรก เกิด error ตรงบรรทัด
DataGridView1.DataBind()
แล้วขึ้นว่า 'DataBind is not a mmember of 'System.Forms.DataGridview.''
ผมเลย comment มันออกไป ปรากฎว่าไม่มี error
แต่พอ run แล้วไม่มี output บน Gridview เลยครับ
Code (VB)
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim objConn As System.Data.SqlClient.SqlConnection
Dim objCmd As System.Data.SqlClient.SqlCommand
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim strConnString As String
strConnString = "Data Source=PC-WAT\SQLEXPRESS;Initial Catalog=mydatabase;Integrated Security=True;"
objConn = New System.Data.SqlClient.SqlConnection(strConnString)
objConn.Open()
BindData()
End Sub
Sub BindData()
Dim strSQL As String
strSQL = "SELECT * FROM customer"
Dim dtReader As System.Data.SqlClient.SqlDataReader
objCmd = New System.Data.SqlClient.SqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
'*** BindData to Repeater ***'
DataGridView1.DataSource = dtReader
'DataGridView1.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
End Class
Date :
2012-01-17 10:05:27
By :
zoberranger
เด๋วขอลองดูก่อนครับ
Date :
2012-01-17 10:44:09
By :
zoberranger
Load balance : Server 05