我正在使用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
答案 0 :(得分: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