2015-05-04 42 views
0

我無法填充我的DataGridView。我的代碼三位加下劃線的藍色:從DataSet中填充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 

有人可以突出顯示我的錯誤嗎?

+0

表單中表格的名稱是什麼?您需要指定'SQL.SQLCon',因爲它屬於'SQLControl'類。另外,對於'Dim LoadAdapter As New SqlDataAdapter',你是否將'System.Data.SqlClient'導入到你的表單中? –

回答

0

有幾件事情。

  1. 您需要導入System.Data.SqlClient到您的Form使用你在一個地方使用dgv.Rows.Clear()然後你使用dvg.Rows.Add(RowsCount)上下行表現在以下幾個

    Dim LoadAdapter As New SqlDataAdapter

  2. 。爲什麼你使用dgvdvg來引用網格?什麼是網格的正確名稱?

  3. 當你正在使用的SQLCon變量,它是SQLControl類的私人會員,你需要
    一個。首先使它公開如果你想使用它在形式
    b。當引用變量時使用SQL.SQLCon。 像

    If SQL.SQLCon.State = ConnectionState.Closed Then

  4. ,如果你不使用SQLDS變量爲什麼你甚至需要這個SQL.SQLDS = Nothing

  5. 爲什麼需要Dim LoadAdapter As New SqlDataAdapterDim LoadDataSet As New DataSet因爲你已經有一個SqlDataAdapterDataSetSQLCOntrol類中聲明?您可以簡單地使用SQL. SQLDASQL.SQLDS而不是LoadAdapterLoadDataSet

  6. 您在SQL.SQLDA.Fill(SQL.SQLDS, "GettingInfo")之後有額外的.。刪除額外的.並嘗試。
  7. 此外,我不認爲你DataSet裏面的表的名稱將是GettingInfo。您可能想要使用RowsCount = LoadDataSet.Tables("booking").Rows.Count

會出現很多錯誤,並且在此代碼開始工作之前還有很長的路要走。你會在網上找到大量的教程,像這個one,教你想要達到什麼。

+0

我已經完成了所有的更改,現在我的錯誤顯着減少了。謝謝。但是我仍然有兩個錯誤。這:「SQL.SQLDA.Fill(SQL.SQLDS,」GettingInfo「)。 RowsCount」被加下藍線。並說'RowsCount'不是'Integer'的成員。 – Brooksie

+0

'SQL.SQLDA.Fill(SQL.SQLDS,「GettingInfo」)''後面有一個額外的'.'。刪除多餘的'.'並嘗試 –