我無法填充我的DataGridView。我的代碼三位加下劃線的藍色:從DataSet中填充DataGridView問題
- 「SqlDataAdapter'-它說:‘類型‘的SqlDataAdapter’沒有定義’
- 」 dgv'-它說:‘‘DGV’未聲明’
- 'SQLCon' - 它說: 「 'SQLCon' 未聲明」
我的表單中的代碼:
Public Class Form4
Dim SQL As New SQLControl
Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With DGVData
SQL.SQLDS = Nothing
.Rows.Clear()
.ColumnCount = 3
.Columns(0).HeaderText = "Booking ID"
.Columns(0).Width = 75
.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(0).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(1).HeaderText = "Payment Confirmation"
.Columns(1).Width = 100
.Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(2).HeaderText = "Total Cost"
.Columns(2).Width = 100
.Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
End With
LoadBookingData()
End Sub
Private Sub LoadBookingData()
Dim loadSQL As String = "SELECT * FROM booking"
Dim LoadAdapter As New SqlDataAdapter
Dim LoadDataSet As New DataSet
Dim RowsCount As Integer
dgv.Rows.Clear()
If SQLCon.State = ConnectionState.Closed Then
SQLCon.open()
LoadAdapter.fill(LoadDataSet, "GettingInfo").
RowsCount = LoadDataSet.Tables("GettingInfo").Rows.Count
If RowsCount < 1 Then
MsgBox("There is no records", MsgBoxStyle.Critical, "Sorry")
LoadDataSet.Reset()
Con.Close()
Else
' there are records !
dvg.Rows.Add(RowsCount)
For i As Integer = 0 To RowsCount - 1
With dvg
.Rows(1).Cells(0).Value = LoadDataSet.Tables("GettingInfo").Rows(i).Item("bookingID")
.Rows(1).Cells(0).Value = LoadDataSet.Tables("GettingInfo").Rows(i).Item("paymentConfirmation")
.Rows(1).Cells(0).Value = LoadDataSet.Tables("GettingInfo").Rows(i).Item("totalCost")
End With
Next
End If
LoadDataSet.Reset()
Con.Close()
Else
' the connection is already open
LoadAdapter.fill(LoadDataSet, "GettingInfo").
RowsCount = LoadDataSet.Tables("GettingInfo").Rows.Count
If RowsCount < 1 Then
MsgBox("There is no records", MsgBoxStyle.Critical, "Sorry")
LoadDataSet.Reset()
Con.Close()
Else
' there are records !
dvg.Rows.Add(RowsCount)
For i As Integer = 0 To RowsCount - 1
With dvg
.Rows(1).Cells(0).Value = LoadDataSet.Tables("GettingInfo").Rows(i).Item("bookingID")
.Rows(1).Cells(0).Value = LoadDataSet.Tables("GettingInfo").Rows(i).Item("paymentConfirmation")
.Rows(1).Cells(0).Value = LoadDataSet.Tables("GettingInfo").Rows(i).Item("totalCost")
End With
Next
End If
LoadDataSet.Reset()
Con.Close()
End If
End Sub
我 'SQLControl.vb' 我認爲有些事情與它有關:
Imports System.Data.SqlClient
Public Class SQLControl
Private SQLCon As New SqlConnection With {.ConnectionString = "Data Source=JENNIFER\DDAP2015;Initial Catalog=zachtravelagency;Integrated Security=True;"}
Private SQLcmd As SqlCommand
Public SQLDA As SqlDataAdapter
Public SQLDS As DataSet
有人可以突出顯示我的錯誤嗎?
表單中表格的名稱是什麼?您需要指定'SQL.SQLCon',因爲它屬於'SQLControl'類。另外,對於'Dim LoadAdapter As New SqlDataAdapter',你是否將'System.Data.SqlClient'導入到你的表單中? –