2011-09-20 50 views
0

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

   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 
+0

为什么你有重复的“我”变量? – StingyJack

回答

0

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

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 
相关问题