我目前正在visual studio中创建一个DataGridView。整个过程工作正常,我没有错误。除了在点击“开始”时执行时,只显示一列。并显示在表格中。
例如,在我的流程中,只有"总费用"列显示在" bookingID"我的DataGridView中的行。我希望显示所有三列,如下面的代码所示。这三行是" bookingID"," paymentConfirmation"和" totalCost"。
以下是代码:
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Form4
Dim SQL As New SQLControl
Dim sqlCon As New SqlConnection
Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With DGVData
.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
Public Sub LoadBookingData()
Dim loadSQL As String = "SELECT * FROM booking"
Dim RowsCount As Integer
If SQL.SQLCon.State = ConnectionState.Closed Then
SQL.RunQuery(loadSQL)
RowsCount = SQL.SQLDS.Tables(0).Rows.Count
MsgBox(RowsCount)
' there are records !
DGVData.Rows.Add(RowsCount)
For i As Integer = 0 To RowsCount - 1
With DGVData
.Rows(i).Cells(0).Value = SQL.SQLDS.Tables(0).Rows(i).Item("bookingID")
.Rows(i).Cells(0).Value = SQL.SQLDS.Tables(0).Rows(i).Item("paymentConfirmation")
.Rows(i).Cells(0).Value = SQL.SQLDS.Tables(0).Rows(i).Item("totalCost")
End With
Next
Else
MsgBox("There is no records", MsgBoxStyle.Critical, "Sorry")
SQL.SQLDS.Reset()
SQL.SQLCon.Close()
End If
SQL.SQLDA.Fill(SQL.SQLDS, "GettingInfo")
RowsCount = SQL.SQLDS.Tables("GettingInfo").Rows.Count
If RowsCount < 1 Then
MsgBox("There is no records", MsgBoxStyle.Critical, "Sorry")
SQL.SQLDS.Reset()
SQL.SQLCon.Close()
Else
' there are records !
DGVData.Rows.Add(RowsCount)
For i As Integer = 0 To RowsCount - 1
With DGVData
.Rows(i).Cells(0).Value = SQL.SQLDS.Tables("GettingInfo").Rows(i).Item("bookingID")
.Rows(i).Cells(0).Value = SQL.SQLDS.Tables("GettingInfo").Rows(i).Item("paymentConfirmation")
.Rows(i).Cells(0).Value = SQL.SQLDS.Tables("GettingInfo").Rows(i).Item("totalCost")
End With
Next
End If
SQL.SQLDS.Reset()
SQL.SQLCon.Close()
End Sub
这是SQLControl中的查询:
Imports System.Data.SqlClient
Public Class SQLControl
Public SQLCon As New SqlConnection With {.ConnectionString = "Data Source=JENNIFER\DDAP2015;Initial Catalog=zachtravelagency;Integrated Security=True;"}
Public SQLcmd As SqlCommand
Public SQLDA As SqlDataAdapter
Public SQLDS As DataSet
Public Function HasConnection() As Boolean
Try
SQLCon.Open()
SQLCon.Close()
Return True
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return False
End Function
Public Sub RunQuery(Query As String)
Try
SQLCon.Open()
' CREATE COMMAND
SQLcmd = New SqlCommand(Query, SQLCon)
' FILL DATASET
SQLDA = New SqlDataAdapter(SQLcmd)
SQLDS = New DataSet
SQLDA.Fill(SQLDS)
SQLCon.Close()
Catch ex As Exception
MsgBox(ex.Message)
' MAKE SURE CONNECTION IS CLOSED
If SQLCon.State = ConnectionState.Open Then
SQLCon.Close()
End If
End Try
End Sub
关于如何解决这个问题的代码示例将非常感激。谢谢。
答案 0 :(得分:0)
很难对.Net的所有功能进行分类,以确定使用哪些功能。您正在编写许多可以移动到库中的代码。
以下两个函数()为我节省了大量时间:
$(".price").each(function(){
var newLink = $(this).clone();
$(newLink).insertAfter('.title');
});
GetDataTable返回一个只读表,其加载速度比全功能表快。该表可以用作DataGridView的RecordSource。
有时候,当我使用DataGridView的过滤器功能时,我会使用GetDateView。只需使用返回的DataView作为记录源,然后您可以设置DataView过滤器来控制网格中显示的内容。