2011-09-20 92 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 
相關問題