Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net DataGridView未显示表中的所有列_Vb.net_Datagridview - Fatal编程技术网

Vb.net DataGridView未显示表中的所有列

Vb.net DataGridView未显示表中的所有列,vb.net,datagridview,Vb.net,Datagridview,我目前正在visual studio中创建DataGridView。整个过程运行良好,我没有任何错误。除此之外,只有一列在点击“开始”执行时显示,并显示在表单中 例如,在我的流程中,只有“总成本”列显示在DataGridView的“bookingID”行中。我希望所有三列都显示出来,如下面的代码所示。这三行是“bookingID”、“paymentConfirmation”和“totalCost” 代码如下: Imports System.Data.Sql Imports S

我目前正在visual studio中创建DataGridView。整个过程运行良好,我没有任何错误。除此之外,只有一列在点击“开始”执行时显示,并显示在表单中

例如,在我的流程中,只有“总成本”列显示在DataGridView的“bookingID”行中。我希望所有三列都显示出来,如下面的代码所示。这三行是“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

如果您能在代码中举例说明如何解决这个问题,我们将不胜感激。谢谢

很难对.Net的所有功能进行分类,以确定要使用哪些功能。您正在编写大量可以移动到库中的代码

以下两个函数()为我节省了很多时间:

Function GetDataTable(ByVal SQL As String, Optional ByVal ConnectString As String = "", Optional ByVal SingleRow As Boolean = False) As DataTable ' returns read only Datatable
    Try
        If ConnectString.Length = 0 Then ConnectString = g.OISConnectString
        Using con As New System.Data.SqlClient.SqlConnection(ConnectString)
            Dim rdr As Data.SqlClient.SqlDataReader
            con.Open()
            Dim cmd As New SqlCommand(SQL, con)
            If SingleRow Then
                rdr = cmd.ExecuteReader(CommandBehavior.SingleRow Or CommandBehavior.CloseConnection)
            Else
                rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            End If
            Dim dt As New DataTable
            dt.Load(rdr)
            rdr.Close()
            Return dt
        End Using
    Catch ex As Exception
        ExpLog.LogMsgBox(ex, ex.Message, , "GetDataTable", "SQL: " & SQL)
        Return Nothing
    End Try
End Function
Function GetDataView(ByVal SQL As String, Optional ByVal ConnectString As String = "") As DataView ' returns read only Dataview
    Try
        Dim dt As DataTable
        dt = GetDataTable(SQL, ConnectString)
        If dt.Rows.Count = 0 Then
            Return Nothing
        Else
            Dim dv As New DataView(dt)
            Return dv
        End If
    Catch ex2 As NullReferenceException
        Return Nothing
    Catch ex As Exception
        ExpLog.LogMsgBox(ex, ex.Message, , "GetDataView", "SQL: " & SQL)
        Return Nothing
    End Try
End Function
GetDataTable返回一个只读表,该表的加载速度比全功能表快。该表可用作DataGridView的记录源


有时,当我决定如何使用DataGridView的过滤功能时,我会使用GetDateView。只需使用返回的DataView作为记录源,然后您就可以设置DataView筛选器来控制网格中显示的内容。

不需要逐行发布到DGV-事实上,当有数千行时,这是一个坏主意。将DGV绑定到您填充的表。另外,
SQLControl
type似乎误用了DBObject,可能会泄漏。感谢您提供的有用建议!
Function GetDataTable(ByVal SQL As String, Optional ByVal ConnectString As String = "", Optional ByVal SingleRow As Boolean = False) As DataTable ' returns read only Datatable
    Try
        If ConnectString.Length = 0 Then ConnectString = g.OISConnectString
        Using con As New System.Data.SqlClient.SqlConnection(ConnectString)
            Dim rdr As Data.SqlClient.SqlDataReader
            con.Open()
            Dim cmd As New SqlCommand(SQL, con)
            If SingleRow Then
                rdr = cmd.ExecuteReader(CommandBehavior.SingleRow Or CommandBehavior.CloseConnection)
            Else
                rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            End If
            Dim dt As New DataTable
            dt.Load(rdr)
            rdr.Close()
            Return dt
        End Using
    Catch ex As Exception
        ExpLog.LogMsgBox(ex, ex.Message, , "GetDataTable", "SQL: " & SQL)
        Return Nothing
    End Try
End Function
Function GetDataView(ByVal SQL As String, Optional ByVal ConnectString As String = "") As DataView ' returns read only Dataview
    Try
        Dim dt As DataTable
        dt = GetDataTable(SQL, ConnectString)
        If dt.Rows.Count = 0 Then
            Return Nothing
        Else
            Dim dv As New DataView(dt)
            Return dv
        End If
    Catch ex2 As NullReferenceException
        Return Nothing
    Catch ex As Exception
        ExpLog.LogMsgBox(ex, ex.Message, , "GetDataView", "SQL: " & SQL)
        Return Nothing
    End Try
End Function