在vb.net中,datagridview不显示单元格中的值

发布于 2024-12-05 20:54:37 字数 1101 浏览 4 评论 0原文

我用的是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 am using vb.net. I have following weird problems:
If I comment DGVCusClient.Rows.Add(), the cell in ("column1",0) does not display data. But in debug, I can see that the first cell has data assigned.
If I do not comment DGVCusClient.Rows.Add(), the cell in ("column1",0) displays its data correctly. However, it adds the row on the top for the first time. Except for the first row, it adds rows to the bottom as usual.

                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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情场扛把子 2024-12-12 20:54:37

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

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

The following code can always add rows to the bottom, which is what I want. So I just need to first add new row, and then set values to current cells.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文