从DataSet问题填充DataGridView

布鲁克西

我在填满DataGridView时遇到了麻烦。我有三位带蓝色下划线的代码:

  1. 'SqlDataAdapter'-说“类型'SqlDataAdapter'未定义”
  2. 'dgv'-表示“未声明'dgv'”
  3. '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

有人可以强调我的错误吗?

萨加尔·埃里亚斯·杰基(Saagar Elias Jacky)

有几件事。

  1. 您需要导入System.Data.SqlClientForm使用以下内容

    Dim LoadAdapter As New SqlDataAdapter

  2. dgv.Rows.Clear()在一个地方使用,然后看到您正在使用dvg.Rows.Add(RowsCount)线。为什么使用dgvdvg引用网格?网格的正确名称是什么?

  3. 由于使用的SQLCon变量是Private成员SQLControl,因此需要
    a。如果要在表单b中使用它,请首先将其公开
    使用SQL.SQLCon时,你指的是可变的。喜欢

    If SQL.SQLCon.State = ConnectionState.Closed Then

  4. SQL.SQLDS = Nothing如果不使用SQLDS变量,为什么还要用到它呢?

  5. 为什么你需要Dim LoadAdapter As New SqlDataAdapterDim LoadDataSet As New DataSet你已经有一个SqlDataAdapterDataSet在声明的SQLCOntrol类?您可以简单地使用SQL. SQLDAandSQL.SQLDS代替LoadAdapterand LoadDataSet

  6. .结束之后你还有额外的SQL.SQLDA.Fill(SQL.SQLDS, "GettingInfo")删除多余的部分,.然后尝试。
  7. 另外,我认为您内部的表格名称DataSet不会是GettingInfo您可能要使用RowsCount = LoadDataSet.Tables("booking").Rows.Count

会有很多错误,并且您需要很长的路要走才能开始运行此代码。你会发现一吨教程在网上,像这样的一个,教你正在努力实现的目标。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章