【问题标题】:In vb.net, datagridview does not display value in cell在 vb.net 中,datagridview 不在单元格中显示值
【发布时间】:2011-09-20 20:21:32
【问题描述】:

我正在使用 vb.net。我有以下奇怪的问题: 如果我注释 DGVCusClient.Rows.Add(),则 ("column1",0) 中的单元格不显示数据。但是在调试中,我可以看到第一个单元格分配了数据。 如果我不评论 DGVCusClient.Rows.Add(),则 ("column1",0) 中的单元格会正确显示其数据。但是,它第一次在顶部添加了行。除了第一行,它像往常一样在底部添加行。

                Dim i As Integer = DGVCusClient.CurrentRow.Index
    If Not ContainRecord(tempCusid, tempCltid) Then
        Dim i As Integer = DGVCusClient.CurrentRow.Index

        DGVCusClient.Item("Column1", i).Value = "a"
        DGVCusClient.Item("Column2", i).Value = "b"
                         'DGVCusClient.Rows.Add()

    End If

Private Function ContainRecord(ByVal strCusid As String, ByVal strCltid As String) As Boolean
    For i As Integer = 0 To DGVCusClient.Rows.Count - 1
        If Not DGVCusClient.Item("Column1", i).Value Is Nothing AndAlso Not DGVCusClient.Item("Column2", i).Value Is Nothing Then
            If DGVCusClient.Item("Column1", i).Value.ToString = strCusid AndAlso DGVCusClient.Item("Column2", i).Value.ToString = strCltid Then
                Return True
            End If
        End If
    Next

    Return False
End Function

【问题讨论】:

  • 为什么有重复的“i”变量?

标签: vb.net datagridview


【解决方案1】:

下面的代码总是可以在底部添加行,这正是我想要的。所以我只需要先添加新行,然后为当前单元格设置值。

If Not ContainRecord("a", "b") Then
Dim i As Integer = DGVCusClient.CurrentRow.Index
DGVCusClient.Rows.Add()
DGVCusClient.Item("Column1", i).Value = "a"   
DGVCusClient.Item("Column2", i).Value = "b" 
End If

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    相关资源
    最近更新 更多