Datagridview-Rows的奇怪问题未显示

时间:2016-10-03 09:50:21

标签: vb.net datagridview refresh

我有一个Datagridview连接到数据集。问题是偶尔,当数据刷新时,它不会显示在DGV中。代码是:

Private Sub DisplayInDGV()
    Dim SQLSet As String
    Dim DASet As New OleDb.OleDbDataAdapter
    Dim DSSet As New DataSet
    SQLSet = "Select * From SetDisplayTable"

    DASet = New OleDb.OleDbDataAdapter(SQLSet, Con)
    DSSet.Clear()
    DASet.Fill(DSSet, "DSSetHere")
    With DGVSetView

        .Refresh()
        .AutoGenerateColumns = False   
        .DataSource = Nothing
        .DataSource = DSSet.Tables(0)

        .Update()
       DGVSetView.Columns(i).DataPropertyName = DSSet.Tables(0).Columns(i).ToString

        .Columns(0).DataPropertyName = DSSet.Tables(0).Columns(0).ToString
        .Columns(2).DataPropertyName = DSSet.Tables(0).Columns(1).ToString
        .Columns(3).DataPropertyName = DSSet.Tables(0).Columns(2).ToString
        .Columns(4).DataPropertyName = DSSet.Tables(0).Columns(3).ToString
        .Columns(5).DataPropertyName = DSSet.Tables(0).Columns(4).ToString
        .Columns(6).DataPropertyName = DSSet.Tables(0).Columns(5).ToString
        .Columns(7).DataPropertyName = DSSet.Tables(0).Columns(6).ToString
        .Columns(8).DataPropertyName = DSSet.Tables(0).Columns(7).ToString
        .Columns(9).DataPropertyName = DSSet.Tables(0).Columns(8).ToString
        .Columns(10).DataPropertyName = DSSet.Tables(0).Columns(9).ToString
        .Columns(11).DataPropertyName = DSSet.Tables(0).Columns(10).ToString 'Item Unique Code for Hot Edit
        .Columns(14).DataPropertyName = DSSet.Tables(0).Columns(12).ToString
    End With
    'Updating Totals/::
    For ItemRow As Integer = 0 To DGVSetView.Rows.Count - 1
        If DGVSetView.Rows(ItemRow).Cells(14).Value = True Then
            DGVSetView.Rows(ItemRow).Cells(12).Value = DGVSetView.Rows(ItemRow).Cells(10).Value
        ElseIf DGVSetView.Rows(ItemRow).Cells(14).Value = False Then
            DGVSetView.Rows(ItemRow).Cells(13).Value = DGVSetView.Rows(ItemRow).Cells(10).Value

        End If
    Next
    'Updating School and general totals in DGV//:
    Dim SchoolTotal, GeneralTotal As Decimal
    For ColumnTotal As Integer = 0 To DGVSetView.Rows.Count - 1

        SchoolTotal += DGVSetView.Rows(ColumnTotal).Cells(12).Value
        GeneralTotal += DGVSetView.Rows(ColumnTotal).Cells(13).Value
    Next
    txtSchoolAmtFinal.Text = SchoolTotal
    txtGeneralAmtFinal.Text = GeneralTotal

    DGVSetView.Update()
    'Get gross total of the DGV amount column//:
    If DGVSetView.RowCount <> 0 Then
        Dim GrossAmt As Decimal = 0

        For Index As Integer = 0 To DGVSetView.RowCount - 1
            '  GrossAmt += Convert.ToDecimal(DataGridView1.Rows(Index).Cells(11).Value)
            If Str(DGVSetView.Rows(Index).Cells(10).Value) = "Null" Or (DGVSetView.Rows(Index).Cells(10).Value) <= 0 Then
                MsgBox("Item Number " & (DGVSetView.Rows(Index).Cells(10).Value) & " is either blank or 0", MsgBoxStyle.Exclamation, "Item Error")
            Else
                GrossAmt += Convert.ToDecimal(DGVSetView.Rows(Index).Cells(10).Value)
            End If
        Next
        txtInsertGrossAmt.Text = GrossAmt ' - Val(DGVSetView.Text)
        Call SetNetAmount()
    End If
    'Generate Serial//:
    Dim X As Integer
    Do While X < DGVSetView.Rows.Count
        DGVSetView.Item(0, X).Value = X + 1
        X = X + 1
    Loop
    'Disbaling editing in all columns except pending//:
    With DGVSetView
        .Columns(0).ReadOnly = True
        .Columns(2).ReadOnly = True
        .Columns(3).ReadOnly = True
        .Columns(4).ReadOnly = True
        .Columns(5).ReadOnly = True
        .Columns(6).ReadOnly = True
        .Columns(7).ReadOnly = True
        .Columns(8).ReadOnly = True
        .Columns(9).ReadOnly = True
        .Columns(10).ReadOnly = True
    End With
    txtTotalItems.Text = DGVSetView.Rows.Count
    For Each r As DataGridViewRow In DGVSetView.Rows
        r.Cells(1).Value = True
    Next r
End Sub

问题是偶尔,DGV不会显示任何行并显示空白帧。在这种情况下。如果我检查DGV.Rows.count 尽管表中有基础数据,但结果为0。 请注意,这是随机发生的。其他时候DGV正确刷新并正确显示数据。 另请注意,此DGV位于TabControl中。 此外,当DGV未能显示数据时,我在上述子过程中计算的总数返回零值。因此,问题似乎位于未插入DGV的行中的某处。 谢谢。

哈立德。 //编辑;代码更新: @jmcilhinney我已经更新了我的代码如下。但是,DGV的早期问题偶尔会持续存在。而且更新过程也略有放缓。似乎我在放置Suspend和ResumeBinding语句的位置上犯了一个错误:

Private Sub SetPreview()
    Dim SQLSet As String
    Dim DASet As New OleDb.OleDbDataAdapter
    Dim DSSet As New DataSet
    SQLSet = "Select * From SetDisplayTable"
    Dim DTDGV As New DataTable
    Dim DGVBindingSource As New BindingSource
    DASet = New OleDb.OleDbDataAdapter(SQLSet, Con)

    DASet.Fill(DTDGV)
    DGVBindingSource.DataSource = DTDGV     
    DGVBindingSource.ResumeBinding()

    With DGVSetView
        .AutoGenerateColumns = False
        .DataSource = DGVBindingSource
        .Columns(0).DataPropertyName = DTDGV.Columns(0).ToString
        .Columns(2).DataPropertyName = DTDGV.Columns(1).ToString
        .Columns(3).DataPropertyName = DTDGV.Columns(2).ToString
        .Columns(4).DataPropertyName = DTDGV.Columns(3).ToString
        .Columns(5).DataPropertyName = DTDGV.Columns(4).ToString
        .Columns(6).DataPropertyName = DTDGV.Columns(5).ToString
        .Columns(7).DataPropertyName = DTDGV.Columns(6).ToString
        .Columns(8).DataPropertyName = DTDGV.Columns(7).ToString
        .Columns(9).DataPropertyName = DTDGV.Columns(8).ToString
        .Columns(10).DataPropertyName = DTDGV.Columns(9).ToString
        .Columns(11).DataPropertyName = DTDGV.Columns(10).ToString 'Item Unique Code for Hot Edit
        .Columns(14).DataPropertyName = DTDGV.Columns(12).ToString
    End With

    DGVBindingSource.SuspendBinding()

    'Updating Totals/::
    For ItemRow As Integer = 0 To DGVSetView.Rows.Count - 1
        If DGVSetView.Rows(ItemRow).Cells(14).Value = True Then
            DGVSetView.Rows(ItemRow).Cells(12).Value = DGVSetView.Rows(ItemRow).Cells(10).Value
        ElseIf DGVSetView.Rows(ItemRow).Cells(14).Value = False Then
            DGVSetView.Rows(ItemRow).Cells(13).Value = DGVSetView.Rows(ItemRow).Cells(10).Value

        End If
    Next

    'Updating School and general totals in DGV//:
    Dim SchoolTotal, GeneralTotal As Decimal
    For ColumnTotal As Integer = 0 To DGVSetView.Rows.Count - 1

        SchoolTotal += DGVSetView.Rows(ColumnTotal).Cells(12).Value
        GeneralTotal += DGVSetView.Rows(ColumnTotal).Cells(13).Value
    Next
    txtSchoolAmtFinal.Text = SchoolTotal
    txtGeneralAmtFinal.Text = GeneralTotal

    DGVSetView.Update()
    'Get gross total of the DGV amount column//:
    If DGVSetView.RowCount <> 0 Then
        Dim GrossAmt As Decimal = 0

        For Index As Integer = 0 To DGVSetView.RowCount - 1
            '  GrossAmt += Convert.ToDecimal(DataGridView1.Rows(Index).Cells(11).Value)
            If Str(DGVSetView.Rows(Index).Cells(10).Value) = "Null" Or (DGVSetView.Rows(Index).Cells(10).Value) <= 0 Then
                MsgBox("Item Number " & (DGVSetView.Rows(Index).Cells(10).Value) & " is either blank or 0", MsgBoxStyle.Exclamation, "Item Error")
            Else
                GrossAmt += Convert.ToDecimal(DGVSetView.Rows(Index).Cells(10).Value)
            End If
        Next
        txtInsertGrossAmt.Text = GrossAmt ' - Val(DGVSetView.Text)
        Call SetNetAmount()
    End If

    'Disabling editing in all columns except pending//:
    With DGVSetView
        .Columns(0).ReadOnly = True
        .Columns(2).ReadOnly = True
        .Columns(3).ReadOnly = True
        .Columns(4).ReadOnly = True
        .Columns(5).ReadOnly = True
        .Columns(6).ReadOnly = True
        .Columns(7).ReadOnly = True
        .Columns(8).ReadOnly = True
        .Columns(9).ReadOnly = True
        .Columns(10).ReadOnly = True
    End With
    txtTotalItems.Text = DGVSetView.Rows.Count
    For Each r As DataGridViewRow In DGVSetView.Rows
        r.Cells(1).Value = True
    Next r

End Sub

0 个答案:

没有答案