我在填满DataGridView时遇到了麻烦。我有三位带蓝色下划线的代码:
我表单中的代码:
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
有人可以强调我的错误吗?
有几件事。
您需要导入System.Data.SqlClient
到Form
使用以下内容
Dim LoadAdapter As New SqlDataAdapter
您dgv.Rows.Clear()
在一个地方使用,然后看到您正在使用dvg.Rows.Add(RowsCount)
线。为什么使用dgv
和dvg
引用网格?网格的正确名称是什么?
由于使用的SQLCon
变量是类的Private成员SQLControl
,因此需要
a。如果要在表单b中使用它,请首先将其公开
。使用SQL.SQLCon
时,你指的是可变的。喜欢
If SQL.SQLCon.State = ConnectionState.Closed Then
SQL.SQLDS = Nothing
如果不使用SQLDS
变量,为什么还要用到它呢?
为什么你需要Dim LoadAdapter As New SqlDataAdapter
和Dim LoadDataSet As New DataSet
你已经有一个SqlDataAdapter
和DataSet
在声明的SQLCOntrol
类?您可以简单地使用SQL. SQLDA
andSQL.SQLDS
代替LoadAdapter
and LoadDataSet
。
.
结束之后你还有额外的钱SQL.SQLDA.Fill(SQL.SQLDS, "GettingInfo")
。删除多余的部分,.
然后尝试。DataSet
不会是GettingInfo
。您可能要使用RowsCount = LoadDataSet.Tables("booking").Rows.Count
会有很多错误,并且您需要很长的路要走才能开始运行此代码。你会发现一吨教程在网上,像这样的一个,教你正在努力实现的目标。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句